Question

I'm reading an array of ALAsset objects (delcared as NSMutableArray *assets; in the header file). I'm trying to follow this example in order to print the contents of an ALAssets dictionary to file to be retrieved later. Note that I can properly write the dictionary key to file (commented out), but not the value.

Here is my code which runs at the end of the viewDidLoad() method:

// 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.

The log looks like this:

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

2011-11-17 23:44:41.361 PhotoApp[542:12803] -[NSURL writeToFile:atomically:]: unrecognized selector sent to instance 0x6455650

2011-11-17 23:44:41.370 PhotoApp[542:12803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL writeToFile:atomically:]: unrecognized selector sent to instance 0x6455650'


Edit: Not sure whether it matters, but the project is targeted for iPad, and is running on the simulator.

Was it helpful?

Solution

The method writeToFile:atomically: is available only for NSDictionary and NSArray. Actually you have NSURLs objects in the aDict dictionary. Even for NSString, that has been deprecated actually.

Since you are obtaining the URLs, get the NSURL's absoluteString (which returns NSString) from them and write them to file using writeToFile:atomically:encoding:error:

Otherwise you can write the aDict (NSDictionary) object itself to a file.

OTHER TIPS

The problem is [[aDict valueForKey:key] writeToFile:dictPath atomically:YES]; line.

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

you get the key as public.jpeg and value as

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

which is a NSURL object so when you use.

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

code then [aDict valueForKey:key] return NSURL object which does not recognizes writeToFile: method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top