将任意对象转换为NSString到Swift字符串

将任意对象转换为NSString到Swift字符串,string,swift,nsstring,String,Swift,Nsstring,我试图理解为什么我不能从字典类型的AnyObject中获得swift字符串。如果我使用println(fileCreationDate),它可以工作,但是我需要一个实际的字符串来处理。因此,当我尝试将其转换为NSString时(因为它是一个对象,而不是字符串、结构),它是nil。 以下是我所拥有的: if let atts = fileManager.attributesOfItemAtPath(fileLocation.path!, error:&err) as? Dictionary

我试图理解为什么我不能从字典类型的AnyObject中获得swift字符串。如果我使用println(fileCreationDate),它可以工作,但是我需要一个实际的字符串来处理。因此,当我尝试将其转换为NSString时(因为它是一个对象,而不是字符串、结构),它是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
}
如果将atts=fileManager.attributesFiteMatPath(fileLocation.path!,error:&err)设为?字典{
println(附件)
如果让fileCreationDate:AnyObject=atts[“NSFileCreationDate”]{
println(fileCreationDate)//打印日期
var mystring:NSString!=文件创建日期为?NSString
println(mystring)//打印零
}

谢谢!

您必须使用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
    }
}
如果让atts=NSFileManager.defaultManager().attributesFiteMatPath(myUrl.path!,错误:nil)作为字典{
如果让fileCreationDate=atts[“NSFileCreationDate”]作为?NSDate{
println(文件创建日期)
让mystring=fileCreationDate.description
println(mystring)
}
}
扩展字符串{
var fileExists:Bool{
返回NSFileManager.defaultManager().fileExistsAtPath(self)
}
var fileAttributes:[字符串:AnyObject]{
返回fileExists?NSFileManager.defaultManager().AttributesFiteMatPath(self,错误:nil)作为字典:[:]
}
var fileCreationDate:NSDate{
将fileAttributes[“NSFileCreationDate”]返回为NSDate
}
var fileGroupOwnerAccountName:字符串{
以字符串形式返回fileAttributes[“NSFileGroupOwnerAccountName”]
}
变量文件类型:字符串{
以字符串形式返回fileAttributes[“NSFileType”]
}
var fileHFSTypeCode:Int{
将fileAttributes[“NSFileHFSTypeCode”]返回为Int
}
var fileExtendedAttributes:[字符串:AnyObject]{
将fileAttributes[“NSFileExtendedAttributes”]返回为[字符串:AnyObject]
}
var fileSystemNumber:Int{
将fileAttributes[“NSFileSystemNumber”]返回为Int
}
var fileOwnerAccountName:字符串{
以字符串形式返回文件属性[“NSFileOwnerAccountName”]
}
var fileReferenceCount:Int{
将fileAttributes[“NSFileReferenceCount”]返回为Int
}
var文件修改日期:NSDate{
将文件属性[“NSFileModificationDate”]返回为NSDate
}
var fileExtensionHidden:Bool{
将fileAttributes[“NSFileExtensionHidden”]返回为Bool
}
var fileSize:Int{
将fileAttributes[“NSFileSize”]返回为Int
}
var fileGroupOwnerAccountID:Int{
将fileAttributes[“NSFileGroupOwnerAccountID”]返回为Int
}
var fileOwnerAccountID:Int{
将fileAttributes[“NSFileOwnerAccountID”]返回为Int
}
var filePosixPermissions:Int{
将fileAttributes[“NSFilePosixPermissions”]返回为Int
}
var fileHFSCreatorCode:Int{
将fileAttributes[“NSFileHFSCreatorCode”]返回为Int
}
var fileSystemFileNumber:Int{
将fileAttributes[“NSFileSystemFileNumber”]返回为Int
}
}

您必须使用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
    }
}
如果让atts=NSFileManager.defaultManager().attributesFiteMatPath(myUrl.path!,错误:nil)作为字典{
如果让fileCreationDate=atts[“NSFileCreationDate”]作为?NSDate{
println(文件创建日期)
让mystring=fileCreationDate.description
println(mystring)
}
}
扩展字符串{
var fileExists:Bool{
返回NSFileManager.defaultManager().fileExistsAtPath(self)
}
var fileAttributes:[字符串:AnyObject]{
返回fileExists?NSFileManager.defaultManager().AttributesFiteMatPath(self,错误:nil)作为字典:[:]
}
var fileCreationDate:NSDate{
将fileAttributes[“NSFileCreationDate”]返回为NSDate
}
var fileGroupOwnerAccountName:字符串{
以字符串形式返回fileAttributes[“NSFileGroupOwnerAccountName”]
}
变量文件类型:字符串{
以字符串形式返回fileAttributes[“NSFileType”]
}
var fileHFSTypeCode:Int{
将fileAttributes[“NSFileHFSTypeCode”]返回为Int
}
var fileExtendedAttributes:[字符串:AnyObject]{
将fileAttributes[“NSFileExtendedAttributes”]返回为[字符串:AnyObject]
}
var fileSystemNumber:Int{
将fileAttributes[“NSFileSystemNumber”]返回为Int
}
var fileOwnerAccountName:字符串{
以字符串形式返回文件属性[“NSFileOwnerAccountName”]
}
var fileReferenceCount:Int{
将fileAttributes[“NSFileReferenceCount”]返回为Int
}
var文件修改日期:NSDate{
将文件属性[“NSFileModificationDate”]返回为NSDate
}
var fileExtensionHidden:Bool{
将fileAttributes[“NSFileExtensionHidden”]返回为Bool
}
var fileSize:Int{
将fileAttributes[“NSFileSize”]返回为Int
}
var fileGroupOwnerAccountID:Int{
将fileAttributes[“NSFileGroupOwnerAccountID”]返回为Int
}
var fileOwnerAccountID:Int{
将fileAttributes[“NSFileOwnerAccountID”]返回为Int
}
var filePosixPermissions:Int{
将fileAttributes[“NSFilePosixPermissions”]返回为Int
}
var fileHFSCreatorCode:Int{
将fileAttributes[“NSFileHFSCreatorCode”]返回为Int
}
var fileSystemFileNumber:Int{
将fileAttributes[“NSFileSystemFileNumber”]返回为Int
}
}

谢谢!这很有效。我想知道您是如何知道它是NSDate的,所以我仔细查看了NSFileCreationDate的文档,确定它是NSDate。谢谢!谢谢!这很有效。我想知道您是如何知道它是NSDate的,所以我仔细查看了NSFileCreationDate的文档,确定它是NSDate。谢谢!