简体   繁体   English

使用NSUserDefaults Xcode iPhone的问题

[英]Issues with using NSUserDefaults xcode iphone

Hi here is the code in question and the issues that i am getting, it will compile but has warning attached which i have highlighted. 您好,这里是有问题的代码以及我所遇到的问题,它将编译,但附带有我已强调的警告。

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

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

//ISSUES START ON THIS LINE //问题开始于此行

    [[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 

Your problem is here: 您的问题在这里:

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

there is no method signature for NSNumber that also takes a forKey argument, you are missing a bracket to close NSNumber numberwithint, change to this to fix the issue: NSNumber没有方法签名也带有forKey参数,您缺少括号来关闭NSNumber numberwithint,对此进行更改以解决此问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM