George The Degu George The Degu - 8 months ago 53
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.


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 {

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: