【发布时间】:2017-08-16 06:22:01
【问题描述】:
根据苹果的文档,我们可以为一个文件设置很多属性
一个字典,其中包含要为路径设置的属性作为键,以及属性的相应值作为值。您可以设置以下属性:busy、creationDate、extensionHidden、groupOwnerAccountID、groupOwnerAccountName、hfsCreatorCode、hfsTypeCode、immutable、modificationDate、ownerAccountID、ownerAccountName、posixPermissions。您可以更改单个属性或属性的任意组合;您不需要为所有属性指定键。
我想为一个文件设置一个额外的参数。参数是一个字符串,我找不到任何可以设置字符串的属性。 比如我试过
try FileManager.default.setAttributes([FileAttributeKey.ownerAccountName: NSString(string: "0B2TwsHM7lBpSMU1tNXVfSEp0RGs"), FileAttributeKey.creationDate: date], ofItemAtPath: filePath.path)
但是当我加载键时
let keys = try FileManager.default.attributesOfItem(atPath: filePath.path)
print(keys)
我只更改了 .creationDate
[__C.FileAttributeKey(_rawValue: NSFileType): NSFileTypeRegular,
__C.FileAttributeKey(_rawValue: NSFilePosixPermissions): 420,
__C.FileAttributeKey(_rawValue: NSFileSystemNumber): 16777220,
__C.FileAttributeKey(_rawValue: NSFileReferenceCount): 1,
__C.FileAttributeKey(_rawValue: NSFileGroupOwnerAccountName): staff,
__C.FileAttributeKey(_rawValue: NSFileSystemFileNumber): 8423614,
__C.FileAttributeKey(_rawValue: NSFileGroupOwnerAccountID): 20,
__C.FileAttributeKey(_rawValue: NSFileModificationDate): 2017-08-16 06:03:57 +0000,
__C.FileAttributeKey(_rawValue: NSFileCreationDate): 1970-01-01 00:33:20 +0000,
__C.FileAttributeKey(_rawValue: NSFileSize): 9795,
__C.FileAttributeKey(_rawValue: NSFileExtensionHidden): 0,
__C.FileAttributeKey(_rawValue: NSFileOwnerAccountID): 501]
有什么方法可以将字符串值设置为 FileAttribute?
【问题讨论】:
-
可能是权限问题?只有文件的所有者(和 root)才能更改所有者属性。
-
否,但我尝试使用 root 用户的帐户名,但也无法正常工作。
标签: swift nsfilemanager