【发布时间】:2016-01-12 10:58:25
【问题描述】:
我想测试文件系统上的文件或目录是否有某个标志,在这种情况下是“隐藏”标志,然后设置或删除它。我知道这可以通过命令行实现,但我想知道是否可以使用 Cocoa/Swift 来实现?
我尝试使用NSFileManagerattributesOfItemAtPath,但返回的对象不包含标志。
示例:
let fm = NSFileManager.defaultManager()
do {
let testLibrary = try fm.attributesOfItemAtPath(dataPath)
print (testLibrary)
} catch let error as NSError {
print("JSON Error: \(error.localizedDescription)")
}
返回:
["NSFileCreationDate": 2013-08-16 21:37:57 +0000,
"NSFileGroupOwnerAccountName": staff,
"NSFileType": NSFileTypeDirectory,
"NSFileSystemNumber": 16777220,
"NSFileOwnerAccountName": xjx,
"NSFileReferenceCount": 61,
"NSFileModificationDate": 2015-10-22 07:25:12 +0000,
"NSFileExtensionHidden": 0,
"NSFileSize": 2074,
"NSFileGroupOwnerAccountID": 20,
"NSFileOwnerAccountID": 501,
"NSFilePosixPermissions": 448,
"NSFileSystemFileNumber": 603923]
作为比较,当我在主目录中执行 ls -lO 时,我看到以下内容(注意“库”上的隐藏标志):
drwx------+ 49 xjx staff - 1666 Jan 11 19:43 Downloads
drwx------@ 28 xjx staff - 952 Jan 11 08:40 Dropbox
drwx------@ 61 xjx staff hidden 2074 Oct 22 09:25 Library
drwx------+ 7 xjx staff - 238 Apr 30 2015 Movies
drwx------+ 5 xjx staff - 170 Jun 14 2015 Music
【问题讨论】: