Question

I have class Prefs.m with methods:

+ (void) setString:(NSString *)valor chave:(NSString *)chave{

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    [prefs setObject:valor forKey:chave];

    [prefs synchronize];
}

+ (NSString *) getString:(NSString *)chave{

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    NSString *valor = [prefs stringForKey:chave];

    return valor;
}

And I set the preferences in the class LoginController.m :

//Guarda informações do usuário no NSUserDefault
  [Prefs setString:strID chave:@"prefID"];
  [Prefs setString:usrNome chave:@"prefNome"];
  [Prefs setString:usrEmail chave:@"prefEmail"];

But when I load the prefs in the class IndicationViewController.m :

//Busca por informações do usuário se não tem o ID na prefs
 int intID = [Prefs getInteger:@"prefID"];

Return value = 0. But in the set prefs, the value was 1. Why?

Était-ce utile?

La solution

Change this:

int intID = [Prefs getInteger:@"prefID"];

To this:

int intID = [[Prefs getString:@"prefID"] intValue];

Or to this:

NSString *prefID = [Prefs getString:@"prefID"];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top