0

In my and successBlock below, Im saving the info to my NSMutableArray selectedPhotos. BTW, the info is a ALAsset url. URLs from my camera roll.

     } andSuccessBlock:^(NSArray *info) 
            [self.selectedPhotos setArray:info];

What I needed to do is save it via NSUserDefaults. Here is how I save it in NSUserDefaults:

            [[NSUserDefaults standardUserDefaults] setObject:self.selectedPhotos forKey:@"PickedImage"];
            self.selectedPhotos = [[NSUserDefaults standardUserDefaults] objectForKey:@"PickedImage"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            NSLog(@"SelectedPhotos:: %@", self.selectedPhotos);

But the problem is, when I log it it says NULL. How can I save my array via NSUserDefaults. Thanks for the help.

LOG:

SelectedPhotos:: (null)
*** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '(
    "ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=119A0D2D-C267-4B69-A200-59890B2B0FE5&ext=JPG",
    "ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=92A7A24F-D54B-496E-B250-542BBE37BE8C&ext=JPG",
    "ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=77AC7205-68E6-4062-B80C-FC288DF96F24&ext=JPG" )' of class '__NSArrayM'.  Note that dictionaries and arrays in property lists must also contain only property values.
Bazinga
  • 2,456
  • 33
  • 76

3 Answers3

1

Look at the log you got:

Note that dictionaries and arrays in property lists must also contain only property values.

An ALAsset is not valid for storage in NSUserDefaults. Only NSString, NSNumber, NSData, NSDate, NSArray, and NSDictionary can be stored there.

You must figure out what data from the asset you want to store, and store that. I suspect the URL is all you're really interested in storing, but without knowing what you plan to do with the data, I can't be sure.

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
  • I am using the `AGImagePickerController` which, after picking images and display a checkmark beside it, the checkmark can be retain even when going to different views, but the problem is when the app is exited it is also deallocated. So I need to store that part into a `NSUserDefaults`. So when app restart, the checkmark in the picker can be retained again or redisplay. – Bazinga Oct 08 '12 at 02:50
  • What data from the assets do you think you'd need to implement that? – Jonathan Grynspan Oct 08 '12 at 02:56
  • Here in my question it is connected http://stackoverflow.com/questions/12670025/image-url-saving – Bazinga Oct 08 '12 at 03:29
0

To you save a NSMutableArray with ALAsset into the NSUserDefaults, you need to convert it in a NSData and save. Take a look:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:self.selectedPhotos];
[userDefaults setObject:arrayData forKey:@"selectedPhotos"];

And to log (and read) it, you need simple to:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSData *arrayData = [defaults objectForKey:@"selectedPhotos"];
NSLog(@"%@", [NSKeyedUnarchiver unarchiveObjectWithData:data]);
  • This is factually incorrect. In regular execution, you never *need* to send `-synchronize`. – Jonathan Grynspan Oct 08 '12 at 02:42
  • Take a look at edit, @Superman. Here I'm converting it in a `NSData`. Hope this solves your question. –  Oct 08 '12 at 02:46
  • crashes with Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ALAsset encodeWithCoder:]: – Bazinga Oct 08 '12 at 05:03
0

-Try saving it after archiving it (i.e converting to NSData) .Please have a look at the following answer Why NSUserDefaults failed to save NSMutableDictionary in iPhone SDK?

Community
  • 1
  • 1
Javal Nanda
  • 1,828
  • 3
  • 14
  • 26
  • it might be crashing because you might have forgotten to encoded one of your variable , which you are trying to save in userdefaults – Javal Nanda Oct 08 '12 at 07:35
  • the variable im using is `NSMutableArray` – Bazinga Oct 08 '12 at 07:41
  • ok,it crashes because AlAsset will try to encodeWithCoder. Encoder works in a way that each custom objects encodeWithCoder is called until each primitive types of the custom objects are encoded. Perhaps,in this case you might need to subclass ALAsset to encode URL also.. – Javal Nanda Oct 08 '12 at 07:43
  • try subclassing ALAsset and provide initWithCoder and decodeWithCoder for it – Javal Nanda Oct 08 '12 at 09:01