2017-08-16 5 views
1

属性のファイルするためにいくつかの文字列値を設定し、我々はファイルスウィフトは、Appleのドキュメントによると

属性はパスのためにとに対応する値を値として設定するキーとして含む辞書の属性の多くを設定することができます属性。次の属性を設定できます。ビジー、creationDate、extensionHidden、groupOwnerAccountID、groupOwnerAccountName、hfsCreatorCode、hfsTypeCode、不変、modificationDate、ownerAccountID、ownerAccountName、posixPermissions。単一の属性または任意の属性の組み合わせを変更できます。すべての属性にキーを指定する必要はありません。

ファイルの追加パラメータを設定したいとします。パラメータは文字列です。文字列を設定できる属性は見つかりません。 は例えば、私は

try FileManager.default.setAttributes([FileAttributeKey.ownerAccountName: NSString(string: "0B2TwsHM7lBpSMU1tNXVfSEp0RGs"), FileAttributeKey.creationDate: date], ofItemAtPath: filePath.path) 

を試してみました。しかし、私は鍵をロードするとき、私は唯一の.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] 

を変更します

let keys = try FileManager.default.attributesOfItem(atPath: filePath.path) 
print(keys) 

は、私がFileAttributeに文字列値を設定することができます任意の方法はあります?

+0

権限の問題?ファイル(およびルート)の所有者だけが所有者属性を変更できます。 –

+0

いいえ、私はrootユーザーのアカウント名を使用しようとしましたが、動作しませんでした。 – Alex

答えて

3

ドキュメントには、「次の属性を設定できます」と記載されています。つまり、これらの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)") 
} 
+0

申し訳ありませんが、最後にコメントしたことがあります。 - はい、無効な所有者または許可の問題である可能性があります。 –

+0

あなたのコメントを削除した後、コメントを削除しました。 'FileAttributeKey.ownerAccountName'のために渡されたばかげたユーザID文字列が有効なユーザIDのように見えないので、OSがいくつかのバリデーションを行い、その属性に変更が行われないと思っています。 –

+0

ええ、私は結局一つの解決策を見つけました。 https://stackoverflow.com/questions/38343186/write-extend-file-attributes-swift-example – Alex

関連する問題