0

我正在读取 ALAsset 对象的数组(NSMutableArray *assets;在头文件中删除)。我正在尝试按照此示例将 ALAssets 字典的内容打印到文件以供稍后检索。请注意,我可以正确地将字典键写入文件(已注释掉),但不能正确写入值。

这是我在 viewDidLoad() 方法末尾运行的代码:

// Create path to Documents
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

if ([paths count] > 0) {
    NSString *dictPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"dict.out"];
    NSDictionary *aDict = [[assets objectAtIndex:0] valueForProperty:ALAssetPropertyURLs];

    for (id key in aDict) {
        NSLog(@"key: %@, value: %@", key, [aDict valueForKey:key]);  
        //[key writeToFile:dictPath atomically:YES];        <- THIS LINE WORKS
        [[aDict valueForKey:key] writeToFile:dictPath atomically:YES];            
    }
}
else
    NSLog(@"nope");    // The program doesn't get here.

日志如下所示:

2011-11-17 23:44:41.340 PhotoApp[542:12803] 键:public.jpeg,值:asset-library://asset/asset.JPG?id=1000000001&ext=JPG

2011-11-17 23:44:41.361 PhotoApp[542:12803]-[NSURL writeToFile:atomically:]: 无法识别的选择器发送到实例 0x6455650

2011-11-17 23:44:41.370 PhotoApp[542:12803] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSURL writeToFile:atomically:]:无法识别的选择器发送到实例 0x6455650”


编辑:不确定是否重要,但该项目是针对 iPad 的,并且正在模拟器上运行。

4

2 回答 2

2

问题是 [[aDict valueForKey:key] writeToFile:dictPath atomically:YES];线。

如在key: public.jpeg, value: assets-library://asset/asset.JPG?id=1000000001&ext=JPG

你 得到的关键价值public.jpeg

asassets-library://asset/asset.JPG?id=1000000001&ext=JPG

这是一个NSURL对象,所以当你使用时。

[[aDict valueForKey:key] writeToFile:dictPath atomically:YES];

然后代码[aDict valueForKey:key]返回NSURL不识别writeToFile: 方法的对象。

于 2011-11-18T05:15:53.163 回答
1

该方法writeToFile:atomically:仅适用于 NSDictionary 和 NSArray。实际上,您的aDict字典中有 NSURLs 对象。即使对于 NSString,它实际上已被弃用。

由于您正在获取 URL,因此从它们获取 NSURL 的 absoluteString(返回 NSString)并使用它们将它们写入文件writeToFile:atomically:encoding:error:

否则,您可以将aDict(NSDictionary) 对象本身写入文件。

于 2011-11-18T05:13:19.343 回答