0

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!

1 Answer 1

4

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
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! that worked. I was wondering how you knew it was an NSDate so I looked closer at the documentation for NSFileCreationDate and sure enough it was an NSDate. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.