NSFileManager & NSFilePosixPermissions
First, NSFilePosixPermissions
is the name of a constant. Its value may also be the same, but that’s not guaranteed. The value of the NSFilePosixPermissions
constant could change between framework releases, e. g. from @"NSFilePosixPermissions"
to @"posixPermisions"
. This would break your code. The right way is to use the constant as NSFilePosixPermissions
, not @"NSFilePosixPermissions"
.
Also, the NSFilePosixPermissions reference says about NSFilePosixPermisions
:
The corresponding value is an
NSNumber
object. Use theshortValue
method to retrieve the integer value for the permissions.
The proper way to set POSIX permissions is:
// chmod permissions 777
// Swift
attributes[NSFilePosixPermissions] = 0o777
// Objective-C
[attributes setValue:[NSNumber numberWithShort:0777]
forKey:NSFilePosixPermissions];
Solution in Swift 3
let fm = FileManager.default
var attributes = [FileAttributeKey : Any]()
attributes[.posixPermissions] = 0o777
do {
try fm.setAttributes(attributes, ofItemAtPath: path.path)
}catch let error {
print("Permissions error: ", error)
}