Hi, I have an ALAsset URL saving NSMutableArray ,
"ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=119A0D2D-C267-4B69-A200-59890B2B0FE5&exโโt=JPG", "ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=92A7A24F-D54B-496E-B250-542BBE37BE8C&exโโt=JPG", "ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=77AC7205-68E6-4062-B80C-FC288DF96F24&exโโt=JPG
I could not save NSMutableArray in NSUserDefaults due to an error with the error Note that dictionaries and arrays in property lists must also contain only property values. I am thinking about using this:
- (void)encodeWithCoder:(NSCoder *)encoder { //Encode properties, other class variables, etc [encoder encodeObject:self.selectedPhotos forKey:@"selectedPhotos"]; } - (id)initWithCoder:(NSCoder *)decoder { if((self = [super init])) { //decode properties, other class vars self.selectedPhotos = [decoder decodeObjectForKey:@"selectedPhotos"]; } return self; }
then save and extract it with this code:
- (void)saveCustomObject:(MyCustomObject *)obj { NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:obj]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:myEncodedObject forKey:@"myEncodedObjectKey"]; } - (MyCustomObject *)loadCustomObjectWithKey:(NSString *)key { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSData *myEncodedObject = [defaults objectForKey:key]; MyCustomObject *obj = (MyCustomObject *)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject]; return obj; }
But somehow I didnโt quite understand, it still crashed in my code. Do not know how. And I could not save it to NSUserDefaults. Hope someone helps. In fact, there was a problem with this. I hope someone NSUserDefaults me on the right path to salvation and returns me the right path from NSUserDefaults . Then go back to NSMutableArray .
source share