我想测试文件系统上的文件或目录是否具有特定标志(在本例中为“隐藏”标志),然后设置或删除它。我知道这可以通过命令行实现,但我想知道是否可以使用 Cocoa/Swift 来做到这一点?
我尝试使用NSFileManager
attributesOfItemAtPath
,但返回的对象不包含标志。
example:
let fm = NSFileManager.defaultManager()
do {
let testLibrary = try fm.attributesOfItemAtPath(dataPath)
print (testLibrary)
} catch let error as NSError {
print("JSON Error: \(error.localizedDescription)")
}
returns:
["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