我创建了一个简单的plist文件,其中包含我正在编写的纸牌游戏的一些用户偏好。 我还创建了一个控制器,读取和写入这个plinger文件,这是一个singelton。 一切正常,但经过几次尝试,它停止工作。 将值记录到控制台,它会显示返回值为0的列表,这会导致我的应用程序崩溃 我已经删除了plist并创建了一个新的,然后相同的故事,工作正常2或3次然后繁荣零。
这里是控制器singelton代码的副本:
@implementation userOptionsController
static userOptionsController* _sharedOptionsController = nil;
@synthesize backgroundSound=_backgroundSound;
@synthesize soundEffects = _soundEffects;
@synthesize coach = _coach;
@synthesize numberOfDecks = _numberOfDecks ;
+(userOptionsController*)sharedOptionsController{
@synchronized([userOptionsController class])
{
if(!_sharedOptionsController)
[[self alloc]init];
return _sharedOptionsController;
}
return nil;
}
+(id)alloc
{
@synchronized ([userOptionsController class])
{
NSAssert(_sharedOptionsController == nil, @"Attempted to allocate a second instance of userOptionsController singleton");
_sharedOptionsController = [super alloc];
return _sharedOptionsController;
}
return nil;
}
- (id) init {
self = [super init];
if (self) {
}
return self;
}
-(void)readPlistFile
{
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"playerPrefOptions.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"playerPrefOptions" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
NSMutableDictionary *temp = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
self.backgroundSound = [[temp objectForKey:@"backgroundSounds"]boolValue];
self.soundEffects = [[temp objectForKey:@"soundEffects"]boolValue];
self.coach =[[temp objectForKey:@"coach"]boolValue];
self.numberOfDecks = [[temp objectForKey:@"numberOfDecks"]intValue];
}
-(void)writeOptionsToFile
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"playerPrefOptions.plist"];
NSMutableDictionary *infoDict = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSNumber *moshe = [NSNumber numberWithInt:self.numberOfDecks];
[infoDict setObject: moshe forKey:@"numberOfDecks"];
[infoDict setObject:[NSNumber numberWithBool:self.coach] forKey:@"coach"];
[infoDict setObject:[NSNumber numberWithBool:self.backgroundSound] forKey:@"backgroundSounds"];
[infoDict setObject:[NSNumber numberWithBool:self.soundEffects] forKey:@"soundEffects"];
[infoDict writeToFile:path atomically:YES];
}
@end
所以财产:
int numberOfDecks =[userOptionsController sharedOptionsController].numberOfDecks;
将返回零。
任何想法?
感谢。
答案 0 :(得分:2)
与其使用plist代替此内容,看起来NSUserDefaults
是更合适的位置。
而不是使用默认的plist文件发送应用,而只使用registerDefaults:
发送NSUserDefaults
(通常在您的应用代理application:didFinishLaunchingWithOptions:
中完成)。
然后,无论何时进行任何更改,只需更新NSUserDefaults
并致电synchronize
以保存更改。
答案 1 :(得分:1)
试试这个,看看它做了什么(输出了什么日志):
@implementation userOptionsController
+ (userOptionsController*)sharedOptionsController
{
static dispatch_once_t pred = 0;
__strong static id _sharedObject = nil;
dispatch_once(&pred, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
- (id) init {
self = [super init];
if (self) {
}
return self;
}
-(void)readPlistFile
{
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"playerPrefOptions.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"playerPrefOptions" ofType:@"plist"];
if (![fileManager copyItemAtPath:bundle toPath: path error:&error]) {
NSLog(@"ERROR - file couldn't be copied: %@", error);
}
}
NSMutableDictionary *temp = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
if (temp == nil) {
NSLog(@"ERROR - file couldn't be read");
}
self.backgroundSound = [[temp objectForKey:@"backgroundSounds"]boolValue];
self.soundEffects = [[temp objectForKey:@"soundEffects"]boolValue];
self.coach =[[temp objectForKey:@"coach"]boolValue];
self.numberOfDecks = [[temp objectForKey:@"numberOfDecks"]intValue];
}
-(void)writeOptionsToFile
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"playerPrefOptions.plist"];
NSMutableDictionary *infoDict = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSNumber *moshe = [NSNumber numberWithInt:self.numberOfDecks];
[infoDict setObject: moshe forKey:@"numberOfDecks"];
[infoDict setObject:[NSNumber numberWithBool:self.coach] forKey:@"coach"];
[infoDict setObject:[NSNumber numberWithBool:self.backgroundSound] forKey:@"backgroundSounds"];
[infoDict setObject:[NSNumber numberWithBool:self.soundEffects] forKey:@"soundEffects"];
if (![infoDict writeToFile:path atomically:YES]) {
NSLog(@"ERROR - failed to write the new file (%@)", path);
} else {
NSLog(@"Completed write of:\n%@", infoDict);
}
}
@end