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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
What data from the assets do you think you'd need to implement that?
Here in my question it is connected stackoverflow.com/questions/12670025/image-url-saving
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]);

3 Comments

This is factually incorrect. In regular execution, you never need to send -synchronize.
Take a look at edit, @Superman. Here I'm converting it in a NSData. Hope this solves your question.
crashes with Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ALAsset encodeWithCoder:]:
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?

4 Comments

it might be crashing because you might have forgotten to encoded one of your variable , which you are trying to save in userdefaults
the variable im using is NSMutableArray
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..
try subclassing ALAsset and provide initWithCoder and decodeWithCoder for it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.