I am really new to Xcode and iPhone development. I have written a function in my appDelegate.m file called getISlogged;. It goes something like this:
- (BOOL) getISlogged {
NSUserDefaults *usenow = [NSUserDefaults standardUserDefaults];
NSNumber *islog = nil;
if(usenow){
islog = [usenow objectForKey:@"is_log"]; // should equal 1 or 0...
}
UIAlertView * alert1 = [[UIAlertView alloc] initWithTitle:@"works" message:@"test1" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles: nil];
[alert1 show];
if (islog == [NSNumber numberWithInt:(1)]) {
return YES;
} else {
return NO;
}
}
Ok, now i call it from my viewController.m like so:
SWGAppDelegate *appDelegate = (SWGAppDelegate *)[[UIApplication sharedApplication]delegate];
NSString *islog = @"no";
if(appDelegate.getISlogged){
islog=@"yes";
}
Now when I run it I always get Thread 1: breakpoint 2.1 and i have no idea what do do with that. I have tried removing all the code and leaving just return YES; in the function and I still get the same error =\
Any help or tips would help thank you.
Just simply return the
boolValueof theNSNumberinstance, rather than extracting its integer value, comparing it to 1, and returningYESorNObased on that, e.g. your entire method could be reduced to:If the key does not exist in the user defaults, the result of this method will be
NO.Another “code-smell” is that you are assigning Boolean values to a string (with
@"yes"and@"no"being assigned toislog). There is nothing wrong with that if you simply plan to write the string somewhere, e.g. to a log, but if you are planning on using this variable to determine whether or not to write to a log, then you should instead make it aBOOL. Strings are for storing text, that’s it.Lastly, remember that in Objective-C the
==operator compares object identity (that is, it confirms that two references point to the same object), it doesn’t compare object equality. You can use the built-inisEqual:methods forNSNumberor “extract” the boxed value and compare against another unboxed value, e.g. either of this will do the trick: