【问题标题】:Swift set some string value to file's attributeSwift 为文件的属性设置了一些字符串值
【发布时间】: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


【解决方案1】:

文档说“您可以设置以下属性”。这意味着您只能通过这些 API 设置这些特定属性。

您真正需要的是Extended Attributes,它们使用一组单独的(C 风格)API。

类似:

let directory = NSTemporaryDirectory()
let someExampleText = "some sample text goes here"
do {
    try someExampleText.write(toFile: "\(directory)/test.txt", atomically: true, encoding: .utf8)
} catch let error {
    print("error while writing is \(error.localizedDescription)")
}
let valueString = "setting some value here"
let result = setxattr("\(directory)/test.txt", "com.stackoverflow.test", valueString, valueString.characters.count, 0, 0)

print("result is \(result) ; errno is \(errno)")
if errno != 0
{
    perror("could not save")
}

let sizeOfRetrievedValue = getxattr("\(directory)/test.txt", "com.stackoverflow.test", nil, 0, 0, 0)

var data = Data(count: sizeOfRetrievedValue)

let newResult = data.withUnsafeMutableBytes({
    getxattr("\(directory)/test.txt", "com.stackoverflow.test", $0, data.count, 0, 0)
})

if let resultString = String(data: data, encoding: .utf8)
{
    print("retrieved string is \(resultString)")
}

【讨论】:

  • 抱歉,我不小心删除了我的最后一条评论。 – 是的,这可能是无效的所有者,或权限问题。
  • 是的...在您删除您的评论后,我删除了我的评论。我是说为 FileAttributeKey.ownerAccountName 传入的古怪用户 ID 字符串看起来不像是一个有效的用户 ID,所以我认为操作系统正在进行一些验证,并且不允许该属性发生更改。
  • 是的,我最终找到了一种解决方案。 stackoverflow.com/questions/38343186/…
  • 是的。我试图想出一个getxattr 的例子,结果也用谷歌搜索了同样的相关答案。
猜你喜欢
  • 2014-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
相关资源
最近更新 更多