Yo_Its_Az
Yo_Its_Az

Reputation: 2073

Create if statement using integer

I am pretty new to xcode so forgive my mistakes but I am trying to convert a string into an integer and then create an if statement that makes a button disabled if the value if the integer is less than 15. The integer is stored in a string and I know works correctly because I can display it in a label. I am having trouble converting that string into an integer and then making the button disabled if the score is less than 15. I have no errors, but the code is not working. Here is what I have so far:

- (void)viewDidLoad
{


    NSString *savedValue = [[NSUserDefaults standardUserDefaults]
                            stringForKey:@"levelScore"];


    int level = [savedValue intValue];



    if (level <= 15) {
       levelTwo.enabled = NO;
    }

Any help would be greatly appreciated.

Upvotes: 2

Views: 3559

Answers (2)

CristiC
CristiC

Reputation: 22698

Why not just:

- (void)viewDidLoad
{
    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"levelScore"] <= 15)
       levelTwo.enabled = NO;
}

Upvotes: 2

    - (void)viewDidLoad
    {


        NSString *savedValue = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults]
                                stringForKey:@"levelScore"]];

         or

        NSString *savedValue = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"levelScore"]];



        int level = [savedValue intValue];



        if (level <= 15) {
            levelTwo.enabled = NO;
        }

Upvotes: -1

Related Questions