How to work correctly (retrieve values) using NSUserDefaults

I have a simple method in my code that looks like this:

- (BOOL)isFirstTimeLogin 
{
    NSString *t_gName = 
    [NSString stringWithFormat:@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:kGroupIdentifierKey]];
    if ([t_gName isEqualToString:@""] || t_gName == nil) {
        DLog(@"LoginViewController, m:::isFirstTimeLogin, First time login happening.");
        return YES;
    }

    DLog(@"LoginViewController, m:::isFirstTimeLogin, Not a first time login.");
    return NO;
}

Just what it is, go to the settings package and extract the value from the PSTextFieldSpecifier. If I log in manually and add some arbitrary text, the code will work as expected. However, when I first install the application on a new device, the first condition is false, which should be true. After going through the code, gdb proves that the object is really zero:

(gdb) po t_gName
(Zero)

, , ? , t_gName - / PSTextFieldSpecifier. DefaultValue .

+3
2
[NSString stringWithFormat:@"%@", nil];

- :

@"(null)"

, , nil, @"".

  • stringWithFormat:.
  • string == nil, !string
  • , nil.
  • , t_gName, .

:

- (BOOL)isFirstTimeLogin 
{
    NSString *groupIdentifier = [[NSUserDefaults standardUserDefaults]
                                    objectForKey:kGroupIdentifierKey];
    if (!groupIdentifier) {
        DLog(@"First time login.");
        return YES;
    }

    DLog(@"Not a first time login.");
    return NO;
}
+15

:

 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
     {

        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedFirstTime"])
        {
            // app already launched
            NSLog(@"app already launched");

        }
        else
        {
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedFirstTime"];
            [[NSUserDefaults standardUserDefaults] synchronize];

             NSLog(@"First Time");

            // This is the first launch ever
        }
    }
+1

Source: https://habr.com/ru/post/1704088/


All Articles