Ben Wilson
Ben Wilson

Reputation: 3137

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 

Upvotes: 0

Views: 192

Answers (1)

Oscar Gomez
Oscar Gomez

Reputation: 18488

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:

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

Upvotes: 3

Related Questions