2012-03-11 24 views
6

Tôi muốn sử dụng quyền bát phân (được sử dụng cho chmod) cho NSFilePosixPermissions. Đây là những gì tôi đã làm bây giờ:NSFileManager & NSFilePosixPermissions

NSFileManager *manager = [NSFileManager defaultManager]; 
NSDictionary *atributes; 

[atributes setValue:[NSString stringWithFormat:@"%d", 0777] 
      forKey:@"NSFilePosixPermissions"]; // chmod permissions 777 
[manager setAttributes:atributes ofItemAtPath:@"/Users/lucky/Desktop/script" error:nil]; 

tôi nhận được không có lỗi, nhưng khi tôi kiểm tra kết quả với "ls -o" sự cho phép are't -rwxrwxrwx.

Có vấn đề gì? Cảm ơn bạn đã trợ giúp.

Trả lời

12

Đầu tiên, NSFilePosixPermissions là tên của hằng số. Giá trị của nó cũng có thể giống nhau, nhưng điều đó không được đảm bảo. Giá trị của hằng số NSFilePosixPermissions có thể thay đổi giữa các phiên bản khung, e. g. từ @"NSFilePosixPermissions" đến @"posixPermisions". Điều này sẽ phá vỡ mã của bạn. Cách đúng là sử dụng hằng số là NSFilePosixPermissions, không phải @"NSFilePosixPermissions".

Ngoài ra, các NSFileManager class reference nói về NSFilePosixPermisions:

The corresponding value is an NSNumber object. Use the shortValue method to retrieve the integer value for the permissions.

Cách thích hợp để thiết lập quyền POSIX là:

// chmod permissions 777 

// Swift 
attributes[NSFilePosixPermissions] = 0o777 

// Objective-C 
[attributes setValue:[NSNumber numberWithShort:0777] 
      forKey:NSFilePosixPermissions]; 
+0

Cảm ơn! Chỉ đặt giá trị NSFilePermissions không làm việc tôi đã làm như vậy: 'NSMutableDictionary * attributes = [NSMutableDictionary dictionaryWithDictionary: [thuộc tính managerOfItemAtPath: @" file ... "error: nil]];' Bước tiếp theo tôi đặt quyền và nó đã làm việc ! – qwertz

+2

Bạn cũng có thể sử dụng ký hiệu chữ để chuyển NSNumber (và NSDictionary): '@ {NSFilePosixPermissions: @ 0777}' – Matthematics

+1

Lưu ý rằng số 0 đứng đầu ở đây là rất quan trọng. Điều đó nói với trình biên dịch C rằng hằng số của bạn là trong Octal. Octal 0777 có số thập phân là 511, không cùng số! –

2

Giải pháp trong 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) 
} 
0

Giải pháp trong Swift 4

Tôi đã thử sử dụng 0o777 và điều này không thay đổi tệp thành chỉ đọc. Tôi tin rằng cách thực hiện chính xác như sau:

let fileManager = FileManager.default 

let documentsUrl: URL = self.fileManager.urls(for: .documentDirectory, in: .userDomainMask).first as URL! 

let destinationFileUrl = documentsUrl.appendingPathComponent(fileName) 

let readOnlyAttribute: [FileAttributeKey: Any] = [ 
      .posixPermissions: 0777 
     ] 
do {  

    try fileManager.setAttributes(readOnlyAttribute, ofItemAtPath: destinationFileUrl.path) 

    if fileManager.isWritableFile(atPath: destinationFileUrl.path) { 
     print("writeable") 
    } else { 
     print("read only") 
    } 

} catch let error { 

    print("Could not set attributes to file: \(error.localizedDescription)") 
} 
Các vấn đề liên quan