我需要修改我的文件属性。有一个属性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