根据苹果的文档,我们可以设置很多属性的文件斯威夫特设置一些字符串值文件属性
含有作为键的属性为路径和设置字典的值对应的值属性。您可以设置以下属性:busy,creationDate,extensionHidden,groupOwnerAccountID,groupOwnerAccountName,hfsCreatorCode,hfsTypeCode,immutable,modificationDate,ownerAccountID,ownerAccountName,posixPermissions。您可以更改单个属性或任何属性组合;您无需为所有属性指定键。
我想为文件设置一个额外的参数。该参数是一个字符串,我找不到任何可以设置String的属性。 例如,我试图
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用户的帐户名称,并且也无法正常工作。 – Alex