简体   繁体   English

数据未保存在NSUserDefaults中

[英]Data not save in NSUserDefaults

i having a problem with my NSUserdefaults. 我的NSUserdefaults有问题。 When i type in the name in the textbox and click on the highscore the application will not response anything and the keypad will still be on the screen. 当我在文本框中输入名称并单击高分时,该应用程序将不会响应任何内容,并且键盘仍会出现在屏幕上。 When i tried to load the data, it say that the data for name is nil. 当我尝试加载数据时,它说名称的数据为nil。 The score that i have is 90. Can someone please tell me what is wrong with my coding. 我的分数是90。有人可以告诉我我的编码有什么问题吗?

Lots of thanks. 非常感谢。

-(IBAction)savehighscore_button {


    int i, ii = -1;

    struct high_score {
        NSString *name;
        int highScore;
    };

    struct high_score structArray[10];

    NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];

    for (i=0; i<10; i++) {

        if ([userPreferences stringForKey :[NSString stringWithFormat:@"highScoreNameEntry%d",i]]!=nil && [userPreferences stringForKey :[NSString stringWithFormat:@"highScoreEntry%d"]]!=nil) {
        structArray[i].name= [userPreferences stringForKey:[NSString stringWithFormat:@"highScoreNameEntry%d",i]];
            structArray[i].highScore = [userPreferences integerForKey:[NSString stringWithFormat:@"highScoreEntry%d",i]];
            ii = i;
        }
    }
    if (myScore >0) {
        for (i==ii; i>=0; i--) {
            if (myScore > structArray[i].highScore) {
                if (i<9) {
                    structArray[i+1] = structArray[i];
                    structArray[i].name = nametextbox.text;
                    structArray[i].highScore = myScore;

                    if (i==ii && i<9) {
                        structArray[i+1].name = nametextbox.text;
                        structArray[i+1].highScore = myScore;
                        ii=i+i;
                    }

                    else if(i==ii && i<9) {
                        structArray[i+1].name = nametextbox.text;
                        structArray[i+1].highScore = myScore;
                        ii=i+1;
                    }
                }
            }

            if (ii==-1 && myScore >0) {
                structArray[0].name = nametextbox.text;
                structArray[0].highScore = myScore;
                ii=0;
            }
            for (i=0; i<=ii; i++) {
                [userPreferences setObject:structArray[i].name forKey:[NSString stringWithFormat:@"highScoreNameEntry%d",i]];
                [userPreferences setInteger:structArray[i].highScore forKey:[NSString stringWithFormat:@"highScoreEntry%d",i]];
            }
        }
    }

}

Anything saved into user defaults must be of a standard PLIST-compliant class (NSArray, NSDictionary, NSNumber, NSString, NSDate, NSData, NSValue). 保存到用户默认设置中的所有内容都必须是符合PLIST的标准类 (NSArray,NSDictionary,NSNumber,NSString,NSDate,NSData,NSValue)。 If it's not one of those, it has to be archived as an NSData instance (and therefore be NSCoding compliant) or packed into an NSValue if it's compatible. 如果不是其中之一,则必须将其存档为NSData实例(因此是NSCoding兼容的),或者如果兼容则打包到NSValue中。

That said, you're making this far more difficult on yourself than you need to. 就是说,您使自己面临的困难比您需要做的要困难得多。 Why not have an array (the high score descriptor container) of dictionaries (each is a descriptor for a high score) that hold a name (an NSString) and the score (an NSNumber)? 为什么不具有包含名称(NSString)和分数(NSNumber)的字典(每个分数都是高分数的描述符)的数组(高分数描述符容器)? Each dictionary describes a high score for a given name. 每本词典为给定名称描述高分。 You can then store the whole thing by using -setObject:yourArray forKey:@"HighScores" . 然后,您可以使用-setObject:yourArray forKey:@"HighScores"存储整个内容。

Since you only get immutable copies back from NSUserDefaults, you'll want to ask for a -mutableCopy of the high scores array (don't forget to release it) when you want to modify things. 由于您只能从NSUserDefaults获得不可变的副本,因此,当您要修改内容时,您将需要一个高分数组的-mutableCopy(不要忘记释放它)。 Either replace a score or delete/add. 替换分数或删除/添加。

Using this approach, you don't have to resort to the (sorry for this) ghastly "string#" approach with a fixed number of scores. 使用这种方法,你不必诉诸(对不起,这一点) 可怕的 “串#”的方式有固定数量的分数。 Your array will only contain the existing high scores. 您的数组将仅包含现有的高分。 No waste and all 100% standard Cocoa classes that are fully PLIST'able with no extra work. 没有浪费,并且所有100%标准的可可豆类都是完全PLIST'able,无需额外的工作。 This also lets you easily sort the array using sort descriptors (sorted by the key you used to store the score number in the dictionary). 这也使您可以轻松地使用排序描述符对数组进行排序(按用于将分数存储在字典中的键排序)。

Some basic code: 一些基本代码:

/* Build a fake high scores array */

NSMutableArray * highScores = [NSMutableArray array];

// Bob's score
NSDictionary * bob = [NSDictionary dictionaryWithObjectsAndKeys:
        @"Bob", @"name", 
        [NSNumber numberWithInt:8234323], @"score", 
        nil];
[highScores addObject:bob];

// Jane's score
NSDictionary * jane = [NSDictionary dictionaryWithObjectsAndKeys:
        @"Jane", @"name", 
        [NSNumber numberWithInt:92346345], @"score", 
        nil];
[highScores addObject:jane];

// Donald's score
NSDictionary * donald = [NSDictionary dictionaryWithObjectsAndKeys:
        @"Donald", @"name", 
        [NSNumber numberWithInt:5272348], @"score", 
        nil];
[highScores addObject:donald];

// Sort by high score
NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@"score" 
                                                      ascending:NO];
[highScores sortUsingDescriptors:[NSArray arrayWithObject:sort]];
[sort release];

// Store in user defaults
[[NSUserDefaults standardUserDefaults] setObject:highScores 
                                          forKey:@"HighScores"];

不要忘记使用:

[userPreferences synchronize];

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

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