How do you call FileManager.setAttributes()

Viewed 1193

I'm applying a code snippet from the Realm documentation (https://realm.io/docs/swift/latest/) but it doesn't compile

 try! FileManager.default.setAttributes([FileAttributeKey(rawValue: NSFileProtectionKey): NSFileProtectionNone], ofItemAtPath: folderPath)

What does this error mean

Invalid initializer call with same type 'FileAttributeKey' as parameter

1 Answers

You are trying to initiate a FileAttributeKey with the ObjC NSFileProtectionKey, which is of type NSFileAttributeKey aka same type as FileAttributeKey. Therefore no need to initiate one, just use NSFileProtectionKey on its own.

[NSFileProtectionKey: NSFileProtectionNone]

Depending on your Swift version, you may be told that the keys have been renamed.

let attributes = [ FileAttributeKey.protectionKey : FileProtectionType.none ]

try! FileManager.default.setAttributes(attributes)

The setAttributes method requires a value of type [FileAttributeKey : Any], so if we pass the attributes directly as a dictionary, we can omit FileAttributeKey since the key type is inferred.

try! FileManager.default.setAttributes([.protectionKey: FileProtectionType.none], ofItemAtPath: folderPath)
Related