将 AnyObject 转换为 NSString 为 Swift String
Convert AnyObject to NSString to Swift String
我想了解为什么我无法从我的 AnyObject 字典类型中获取 swift 字符串。如果我执行 println(fileCreationDate) 它会起作用,但我需要某种实际的字符串才能使用。因此,当我尝试将它转换为 NSString(因为它是一个对象,不同于 String,一个结构)时,它是 nil。
这是我拥有的:
if let atts = fileManager.attributesOfItemAtPath(fileLocation.path!, error:&err) as? Dictionary<String, AnyObject> {
println(atts)
if let fileCreationDate:AnyObject = atts["NSFileCreationDate"] {
println(fileCreationDate) //prints a date
var mystring:NSString! = fileCreationDate as? NSString
println(mystring) //prints nil
}
谢谢!
您必须使用 if let 和条件转换为 NSDate 来将您的 anyObject 转换为 NSDate,并且您可以根据需要格式化日期。
if let atts = NSFileManager.defaultManager().attributesOfItemAtPath(myUrl.path!, error:nil) as? Dictionary<String, AnyObject> {
if let fileCreationDate = atts["NSFileCreationDate"] as? NSDate {
println(fileCreationDate)
let mystring = fileCreationDate.description
println(mystring)
}
}
extension String {
var fileExists: Bool {
return NSFileManager.defaultManager().fileExistsAtPath(self)
}
var fileAttributes: [String:AnyObject] {
return fileExists ? NSFileManager.defaultManager().attributesOfItemAtPath(self, error:nil) as Dictionary<String, AnyObject> : [:]
}
var fileCreationDate:NSDate {
return fileAttributes["NSFileCreationDate"] as NSDate
}
var fileGroupOwnerAccountName:String{
return fileAttributes["NSFileGroupOwnerAccountName"] as String
}
var fileType: String {
return fileAttributes["NSFileType"] as String
}
var fileHFSTypeCode: Int {
return fileAttributes["NSFileHFSTypeCode"] as Int
}
var fileExtendedAttributes:[String:AnyObject] {
return fileAttributes["NSFileExtendedAttributes"] as [String:AnyObject]
}
var fileSystemNumber: Int {
return fileAttributes["NSFileSystemNumber"] as Int
}
var fileOwnerAccountName: String {
return fileAttributes["NSFileOwnerAccountName"] as String
}
var fileReferenceCount: Int {
return fileAttributes["NSFileReferenceCount"] as Int
}
var fileModificationDate: NSDate {
return fileAttributes["NSFileModificationDate"] as NSDate
}
var fileExtensionHidden: Bool {
return fileAttributes["NSFileExtensionHidden"] as Bool
}
var fileSize: Int {
return fileAttributes["NSFileSize"] as Int
}
var fileGroupOwnerAccountID: Int {
return fileAttributes["NSFileGroupOwnerAccountID"] as Int
}
var fileOwnerAccountID: Int {
return fileAttributes["NSFileOwnerAccountID"] as Int
}
var filePosixPermissions: Int {
return fileAttributes["NSFilePosixPermissions"] as Int
}
var fileHFSCreatorCode: Int {
return fileAttributes["NSFileHFSCreatorCode"] as Int
}
var fileSystemFileNumber: Int {
return fileAttributes["NSFileSystemFileNumber"] as Int
}
}
我想了解为什么我无法从我的 AnyObject 字典类型中获取 swift 字符串。如果我执行 println(fileCreationDate) 它会起作用,但我需要某种实际的字符串才能使用。因此,当我尝试将它转换为 NSString(因为它是一个对象,不同于 String,一个结构)时,它是 nil。 这是我拥有的:
if let atts = fileManager.attributesOfItemAtPath(fileLocation.path!, error:&err) as? Dictionary<String, AnyObject> {
println(atts)
if let fileCreationDate:AnyObject = atts["NSFileCreationDate"] {
println(fileCreationDate) //prints a date
var mystring:NSString! = fileCreationDate as? NSString
println(mystring) //prints nil
}
谢谢!
您必须使用 if let 和条件转换为 NSDate 来将您的 anyObject 转换为 NSDate,并且您可以根据需要格式化日期。
if let atts = NSFileManager.defaultManager().attributesOfItemAtPath(myUrl.path!, error:nil) as? Dictionary<String, AnyObject> {
if let fileCreationDate = atts["NSFileCreationDate"] as? NSDate {
println(fileCreationDate)
let mystring = fileCreationDate.description
println(mystring)
}
}
extension String {
var fileExists: Bool {
return NSFileManager.defaultManager().fileExistsAtPath(self)
}
var fileAttributes: [String:AnyObject] {
return fileExists ? NSFileManager.defaultManager().attributesOfItemAtPath(self, error:nil) as Dictionary<String, AnyObject> : [:]
}
var fileCreationDate:NSDate {
return fileAttributes["NSFileCreationDate"] as NSDate
}
var fileGroupOwnerAccountName:String{
return fileAttributes["NSFileGroupOwnerAccountName"] as String
}
var fileType: String {
return fileAttributes["NSFileType"] as String
}
var fileHFSTypeCode: Int {
return fileAttributes["NSFileHFSTypeCode"] as Int
}
var fileExtendedAttributes:[String:AnyObject] {
return fileAttributes["NSFileExtendedAttributes"] as [String:AnyObject]
}
var fileSystemNumber: Int {
return fileAttributes["NSFileSystemNumber"] as Int
}
var fileOwnerAccountName: String {
return fileAttributes["NSFileOwnerAccountName"] as String
}
var fileReferenceCount: Int {
return fileAttributes["NSFileReferenceCount"] as Int
}
var fileModificationDate: NSDate {
return fileAttributes["NSFileModificationDate"] as NSDate
}
var fileExtensionHidden: Bool {
return fileAttributes["NSFileExtensionHidden"] as Bool
}
var fileSize: Int {
return fileAttributes["NSFileSize"] as Int
}
var fileGroupOwnerAccountID: Int {
return fileAttributes["NSFileGroupOwnerAccountID"] as Int
}
var fileOwnerAccountID: Int {
return fileAttributes["NSFileOwnerAccountID"] as Int
}
var filePosixPermissions: Int {
return fileAttributes["NSFilePosixPermissions"] as Int
}
var fileHFSCreatorCode: Int {
return fileAttributes["NSFileHFSCreatorCode"] as Int
}
var fileSystemFileNumber: Int {
return fileAttributes["NSFileSystemFileNumber"] as Int
}
}