获取文件的NSURLIsExcludedFromBackupKey的属性

时间:2013-02-19 09:24:10

标签: ios objective-c

要将文件从备份中排除到iCloud和iTunes,我使用了以下代码:

BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]

                              forKey: NSURLIsExcludedFromBackupKey error: &error];

成功的值是YES,但我仍然想检查文件的相应属性。我尝试使用下面的代码,但它失败了:

NSLog(@"%@",[URL valueForKey:NSURLIsExcludedFromBackupKey]);

实现目标的正确方法是什么?谢谢!

1 个答案:

答案 0 :(得分:0)

正如Shashank建议的那样,如果您是通过setResourceValue设置资源值,则需要通过getResourceValue访问它。 valueForKey适用于KVC,与资源值无关。

为此,您需要传入要保存结果的对象。

NSNumber* backupKeyResult = nil;
NSError* error = nil;

BOOL result = [URL getResourceValue:&result forKey:NSURLIsExcludedFromBackupKey error:&error];

if (result && !error) {
   if (backupKeyResult) {
       BOOL backupKeySet = [backupKeyResult boolValue];
       // backupKeySet has the value you've set previously
   }
   else {
        // The requested resource value is not defined for the URL.
   }
}
else {
    if (error) {
        // An error occurred whilst trying this, check your NSError object to see what's up
    }
    else if (!result) {
        // The value was not successfully populated
    }
}