I saved an array using the following code:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myArray];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:#"myArray"];
What is the code to load it back into memory?
Put this where you need to load it, should work. You'll get back an immutable NSData object.
NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:#"myArray"];
Related
I packed an object (NSObject) to (NSData) and then encoded it with (NSASCIIStringEncoding) for sending it to a SQLite database with this code:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:canvasView.trazoYorch];
//convert NSData object to plain text for sending it to DB
NSData *data2 = [[NSString stringWithFormat:#"%#",data] dataUsingEncoding:NSASCIIStringEncoding];
NSString *dataStr = [[NSString alloc] initWithData:data2 encoding:NSASCIIStringEncoding];
everything works ok, but when I want to do the Inverse process NSString to NSData I got different results, this is my code for inverse process
NSString *FirmaString = [self traerFirmadeBD]; //returns the string content of DB
NSData *data2 = [FirmaString dataUsingEncoding:NSASCIIStringEncoding];
FirmaYorch *firmaCompleta = [NSKeyedUnarchiver unarchiveObjectWithData:data2];
Any help solving this I will appreciate
When setting NSUserDefaults, I was initially using this code to set the defaults...
NSMutableArray *array = [NSMutableArray arrayWithObjects: #"string1", #"string2", #"string3", nil];
[[NSUserDefaults standardUserDefaults] setObject:array forKey: #"preset1"];
[[NSUserDefaults standardUserDefaults] synchronize];
I've learned that I should be using this instead:
NSMutableArray *array = [NSMutableArray arrayWithObjects: #"string1", #"string2", #"string3", nil];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:array forKey:#"preset1"];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
Now I'm having an issue manipulating the objects later on in array. Here is the code I use to add/remove strings from array. It worked fine when I was initially setting the defaults manually in my first example. Now, the objects will not remove from the array. I did notice when printing the array in LLDB debugger that array is now being stored as a NSCFArray when it was just an NSArray before.
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"preset1"]];
NSArray *stringsToRemove = #[#"string1", #"string2" ];
for (NSUInteger i = 0; i < stringsToRemove.count; i++) {
[array removeObjectIdenticalTo:[stringsToRemove objectAtIndex:i]];
}
[[NSUserDefaults standardUserDefaults] setObject:array forKey: #"preset1"];
[[NSUserDefaults standardUserDefaults] synchronize];
This code works for me with your setup, after initializing the defaults the second way you described:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *array = [[NSMutableArray alloc] initWithArray: [defaults objectForKey:#"preset1"]];
NSArray *stringsToRemove = [NSArray arrayWithObjects:#"string1", #"string2", nil];
for (NSString *aString in stringsToRemove) {
[array removeObjectIdenticalTo:aString];
}
[defaults setObject:array forKey: #"preset1"];
[defaults synchronize];
So I'm trying to save an image taken from UIImagePickerController to NSUserDefaults so I can open it up in an album I generate, but when I call the delegate method to save the picture, I get the error:
[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '<UIImage: 0x1d0bf0>' of class 'UIImage'. Note that dictionaries and arrays in property lists must also contain only property values.`
The code is as follows:
self.savedPic = [[UIImage alloc] init];
self.savedPic = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[[NSUserDefaults standardUserDefaults] setValue:self.savedPic forKey:[NSString stringWithFormat:#"image_%i",imageCount]];
You should save to disk your image and put the name of your image in the NSUserDefaults, then when needed create it :
[UIImage imageNamed:#"nameOfImage.png"]
It is quite simple, see my codes:
- (UIImage *)backgroundImage {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData* myEncodedImageData = [defaults objectForKey:#"backgroundImageData"];
UIImage* image = [UIImage imageWithData:myEncodedImageData];
return image;
}
- (void)setBackgroundImage:(UIImage *)image {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData* imageData = UIImagePNGRepresentation(image);
[defaults setObject:imageData forKey:#"backgroundImageData"];
[defaults synchronize];
}
if i'm doing this
NSArray * myArray = [[NSArray alloc] init];
myArray = [[NSUserdefaults standardUserDefaults] objectForKey:#"array"];
[myArray release];
am i destroying value stored in NSUserDefaults for key #"array"? Is it still extractable or not already? Does [[NSUserdefaults standardUserDefaults] objectForKey:#"array"]; return pointer or value?
Your call to objectForKey is returning an autoreleased array.
Don't do the alloc/init or release thing around that. You'd be leaking and most likely crashing.
Just do:
NSArray * myArray = [[NSUserDefaults standardUserDefaults] objectForKey: #"array"];
am i destroying value stored in NSUserDefaults for key #"array"?
NO
Is it still extractable or not already?
Extractable
Does [[NSUserdefaults standardUserDefaults] objectForKey:#"array"]; return pointer or value?
Pointer. Returns pointer to object.
Now your code is incorrect. You can rewrite it like this:
NSArray * myArray = [[[NSUserdefaults standardUserDefaults] objectForKey:#"array"] retain];
....
[myArray release];
It sounds to me like you want to get rid of the value. What you are doing is making an array with the data from [[[NSUserdefaults standardUserDefaults] objectForKey:#"array"].
If you want to get the data and use it elsewhere:
NSArray * myArray = [[[NSUserDefaults standardUserDefaults] objectForKey:#"array"] retain];
// do stuff here
[myArray release];
just like beryllium said. Of course, you could have to set it first:
[[NSUserDefaults standardUserDefaults] setObject:anObject forKey:#"array"];
If you want to get rid of the data:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"array"];
Because I honestly do not know what you are skiing, it is very hard to give you the answer you are looking for. Could you please clear it up a little?
Store array
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:yourArray forKey:#"yourKey"];
[userDefaults synchronize];
Retrieve
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *yourArr = [userDefaults objectForKey:#"yourKey"];
I have seen this done the other way. But I have a NSData object returned that is suppose to be a URL of an image file.
How do I do this convert a NSData object into a NSURL?
Thanks
You first need to convert the NSData object to an NSString object and then you can just create a NSURL with the new string.
NSString *urlString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding]; // Or any other appropriate encoding
NSURL *url = [[NSURL alloc] initWithString:[urlString autorelease]];