在Swift中设置文件属性 [英] Setting file attributes in Swift

查看:118
本文介绍了在Swift中设置文件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Swift for OSX中为文件设置文件属性(例如NSCreationDate)?

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

我可以在Objective-C中做到这一点,但无法弄清楚如何在Swift中做到这一点.

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

推荐答案

在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 {
    print(error)
}

确保不要在操场上这样做. Playground仅具有写入其本地文件夹的权限.将其放入Swift项目:

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:

这篇关于在Swift中设置文件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆