¿Qué devuelve [[NSUserDefaults standardUserDefaults] objectForKey]?

if i'm doing this

NSArray * myArray = [[NSArray alloc] init];
myArray = [[NSUserdefaults standardUserDefaults] objectForKey:@"array"];
[myArray release]; 

am i destroying value stored in NSUserDefaults para llave @"array"? Is it still extractable or not already? Does [[NSUserdefaults standardUserDefaults] objectForKey:@"array"]; return pointer or value?

preguntado el 08 de noviembre de 11 a las 17:11

4 Respuestas

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.

Solo haz:

NSArray * myArray = [[NSUserDefaults standardUserDefaults] objectForKey: @"array"];

respondido 08 nov., 11:21

am i destroying value stored in NSUserDefaults for key @"array"?

NO

Is it still extractable or not already?

Extraíble

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]; 

respondido 08 nov., 11:21

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?

respondido 08 nov., 11:22

all i wanted to do is exctract array from nsuserdefaults, set it to another array and leave array stored in nsuserdefaults undestroyed. that's all - Andrey Chernukha

Store array

 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:yourArray forKey:@"yourKey"];  
[userDefaults synchronize];

Recuperar

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *yourArr = [userDefaults objectForKey:@"yourKey"];

respondido 24 nov., 16:16

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.