user3669735
user3669735

Reputation: 13

Convert AnyObject to NSString to Swift String

I am trying to understand why I cannot get a swift String from my dictionary type of AnyObject. If I do a println(fileCreationDate) it works, but I need an actual string of some kind to work with. So when I try to convert it to an NSString (because it is an object unlike String, a struct) it is nil. Here is what I have:

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
}

Thanks!

Upvotes: 0

Views: 2574

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236360

You have to use if let and a conditional cast to NSDate to convert your anyObject to NSDate and them you can format your date as you wish.

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
    }
}

Upvotes: 4

Related Questions