我需要修改我的文件屬性。有一個屬性NSFileGroupOwnerAccountID
哪個蘋果說它可以被修改,但是當我修改它時,它沒有得到更新。 我也嘗試先將權限修改爲0777,但仍然無效。修改NSFileManager中文件的NSFileGroupOwnerAccountID屬性沒有得到更新
代碼:
NSFileManager *filemgr;
NSDictionary *attribs;
NSArray *dirPaths;
NSString *docsDir;
filemgr =[NSFileManager defaultManager];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSArray * filePathsArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: docsDir error:NULL];
NSError *error;
for(int i=0;i<[filePathsArray count];i++)
{
NSString *filePath = [filePathsArray objectAtIndex:i];
NSString *fullPath = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",filePath]];
attribs = [filemgr attributesOfItemAtPath:
docsDir error: NULL];
NSLog (@"POSIX Permissions %@", [attribs objectForKey: NSFilePosixPermissions]);
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:attribs];
[attributes setValue:[NSNumber numberWithShort:0777]
forKey:NSFilePosixPermissions]; // chmod permissions 777
[[NSFileManager defaultManager]setAttributes:attributes ofItemAtPath:docsDir error:&error];
NSLog(@"updated: %@",[filemgr attributesOfItemAtPath:
docsDir error: NULL]);
NSDictionary *dict2 = [filemgr attributesOfItemAtPath:
docsDir error: NULL];
NSMutableDictionary *attributes2 = [NSMutableDictionary dictionaryWithDictionary:dict2];
[attributes2 setValue:[NSNumber numberWithUnsignedLong:12345]forKey:NSFileGroupOwnerAccountID];
[[NSFileManager defaultManager]setAttributes:attributes2 ofItemAtPath:docsDir error:&error];
NSLog(@"updated2: %@",[filemgr attributesOfItemAtPath:
docsDir error: NULL]);
}
「URL」
更新2:
修改僅單個屬性:
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setValue:[NSNumber numberWithUnsignedLong:12345]forKey:NSFileGroupOwnerAccountID];
BOOL check= [[NSFileManager defaultManager]setAttributes:attributes ofItemAtPath:docsDir error:&error];
仍然提示錯誤:
Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x8d0b900 {NSFilePath=/Users/a/Library/Application Support/iPhone Simulator/7.1/Applications/81ADKJ1A2-B612-4784-9524-05456F323212/Documents, NSUnderlyingError=0x8d11b80 "The operation couldn’t be completed. Operation not permitted"}
請建議如何修改屬性。
在此先感謝。
請參閱編輯問題。我試圖做你的建議,但它仍然給出了同樣的錯誤。我也添加了錯誤。請幫忙。 – Gypsa
你是否擁有該文件?您的用戶帳戶是否爲12345組的成員? –
是的我已經使用代碼創建了文件:NSArray * array = [NSArray arrayWithObjects:@「One」, @「Two」, @「Three」, @「Four」,nil]; NSString * filePath = [docsDir stringByAppendingFormat:@「/ myfile.plist」]; [array writeToFile:filePath atomically:YES]; NSLog(@「文件存儲在%@」,filePath);我想將自定義值設置爲任何屬性,因此我嘗試將NSFileGroupOwnerAccountID值修改爲我的隨機值12345.它與任何組都沒有關係。如果我做錯了,請提出建議。 – Gypsa