使用NSUserDefaults xcode iphone的问题

时间:2012-06-19 15:14:50

标签: iphone ios xcode

嗨,这是有问题的代码和我得到的问题, 它会编译但附有警告我已经突出显示。

-(void)viewdidloader
{   
highscore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HIGHSCORE"]integerValue];
}

(void) differnt void
if (score>=highscore) 
    {
    highscore = score;

//问题在这一行开始

    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highscore forKey:@"HIGHSCORE"]];
    highscorelabel.text = [NSString stringWithFormat:@"%i",highscore ];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"NEW HIGH SCORE" message:[NSString stringWithFormat: @"Well done you got a new high score          Level : %d and scored %i points" ,fred,score ] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }


        else 
        {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Lose" message:[NSString stringWithFormat: @"Unlucky you only made it to          Level : %d and scored %i points" ,fred,score ] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
        [alert release];
        }


Issues

NSNumber may not respond to +number:forkey
(message without a matching method signature will be assumed to return "ID" and accept '' as an argument)
NSUserdefaukts may not respond to -setobject 

1 个答案:

答案 0 :(得分:3)

你的问题在这里:

[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highscore forKey:@"HIGHSCORE"]];

NSNumber没有方法签名,它也带有一个forKey参数,你缺少一个括号来关闭NSNumber numberwithint,改为这个来解决问题:

[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highscore] forKey:@"HIGHSCORE"];