George The Degu George The Degu - 1 month ago 9
Swift Question

Setting file attributes in Swift

How would I set the file attributes (such as NSCreationDate) for a file in Swift for OSX?

I can do this in Objective-C but can't figure out how to do it in Swift.

Answer

It's basically the same in Swift. This sets the creation time to the current time:

let path = "/path/to/file"
let attributes = [
    NSFileCreationDate: NSDate()
]

do {
    try NSFileManager.defaultManager().setAttributes(attributes, ofItemAtPath: path)
} catch {
    print(error)
}

Make sure you DON'T do this in playground. Playground only has permission to write to its local folder. Put this into a Swift project:

Comments