1

I have the following code for saving a value and an string. I want to use If statement to check my value.

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:@"Baseball bat" forKey:@"Weaponsname"];
    NSInteger Attack1 = [defaults integerForKey:@"Attack"];

     NSString *Weapon = [defaults stringForKey:@"Weaponsname"];
    if([Weapon  isEqual: @"Baseball bat"]){
        [defaults setInteger:(Attack1+50) forKey:@"Attack"];
        NSLog(@"Baseball bat");
    }


        [defaults synchronize];

So if i have the Baseball bat, the Attack should increase by 50. But it doesn't. What wrong with the code? Thanks

Clarence
  • 1,951
  • 2
  • 34
  • 49
  • Could you try with isEqualToString: ? Oh wait, you should sync after setting "Baseball bat". – EDUsta Aug 09 '14 at 20:28

1 Answers1

2

For string comparison you should use the isEqualToString method found in the NSString class.

if ([Weapon isEqualToString:@"Baseball bat"]) { ... }
jpecoraro342
  • 784
  • 6
  • 6