开发者

Create if statement using integer

开发者 https://www.devze.com 2023-03-19 04:35 出处:网络
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 tha

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 di开发者_如何学Pythonsplay 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.


Why not just:

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


    - (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;
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消