George The Degu George The Degu - 1 year ago 92
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 Source

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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download