【发布时间】:2011-01-09 19:23:06
【问题描述】:
我在这里有一个功能,在完成单轮比赛后,如果您的分数高于默认分数条目或新放置的高分,那么它将与您的数据交换其数据并推低其他所有内容。从列表中删除最后一个条目。目前这只是一种交换,为了功能,我将对其进行硬编码,然后再对其进行重构。
我的主要问题是,当我设置一个文本输入视图来捕获玩家名称时,在没有玩家输入的情况下会立即继续执行并导致游戏崩溃。我注释掉了设置文本的行,因为我有一个默认值,以防我尝试进行的任何尝试失败。如何让执行在输入时等待片刻?我必须设置一个委托方法吗?如果是这样,代表们仍然有点困惑。我可以设置它来工作,但我不明白它,所以我不能用它做任何其他特殊的自定义任务。我已经研究了一段时间,没有进一步......
-(void)saveData:(ScoreKeep *)stats{
NSMutableDictionary *swap = [[NSMutableDictionary alloc]init];//used for swaping entries
NSString *filePath = [self pathOfFile];
NSLog(@"Writing to %@", filePath);
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
NSLog(@"Loading previous dictionary to save...");
dataDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
if ([dataDictionary objectForKey:@"1"]) {
NSMutableDictionary *highScore = [dataDictionary objectForKey:@"1"];
if ([stats.score intValue] > [[highScore objectForKey:@"SCORE"] intValue]) {
NSLog(@"You Win! score: %@ highscore: %@", stats.score,[NSNumber numberWithInt:[[highScore objectForKey:@"SCORE"] intValue]] );
stats = [[ScoreKeep alloc] initWithNibName:@"Scorekeep" bundle:nil];
NSLog(@"Setting up name entry");
[self.view addSubview:stats.view]; //New view is added so that the player can input data(Assume it is complete);
//stats.nameTag = setName.nameTag;//This line is executed before the new view is dismissed causing an error to occur
[stats setupDictionary]; // It just goes down hill from here if the previous line is uncommented
[dataDictionary setObject:stats.sComponents forKey:@"1"];
}else {
NSLog(@"You Lose: %@ highscore: %@", stats.score,[NSNumber numberWithInt:[[highScore objectForKey:@"SCORE"] intValue]] );
}
NSLog(@"Got first place entry");
}else {
NSLog(@"Initilizing Score");
}
}else{
NSLog(@"Creating new dictionary to save...");
dataDictionary = [[NSMutableDictionary alloc]init];
}
[dataDictionary writeToFile:filePath atomically:YES];
}
我们将不胜感激。如果需要更多信息,我很乐意提供。 顺便说一句,ScoreKeep 是一个对象,它包含一个字典和一个创建字典的函数,以便它可以设置我需要的任何值并将它们打包到 sComponents(要输入到主保存文件中的字典)
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@class omphalosUtility;
#pragma mark -
#pragma mark Saving data
#pragma mark -
static inline void poop(){
NSLog(@"POOP");
}
我将尝试制作一个独立于应用程序运行的实用程序文件,以便我可以更新文件并执行其他通用操作,例如在需要时保存。这是朝着我想要采取的方向迈出的一步。
【问题讨论】:
-
请正确格式化您的代码以便显示。我试图修复它,但放弃了所有嵌套的 if。
-
是的,很抱歉它破坏了我修复的代码。
-
顺便说一下乱码,我很抱歉。我计划将所有这些功能构建到一个类中,这样看起来就不会像噩梦一样了。
标签: iphone objective-c saving-data