0

我创建了一个简单的 plist 文件,其中包含一些我正在编写的纸牌游戏的用户偏好。我还创建了一个控制器来读取和写入这个 plist 文件,它是一个单例。一切正常,但经过几次尝试后它停止工作。将值记录到控制台它会显示返回值 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; 

将返回零。

有任何想法吗?

谢谢。

4

2 回答 2

2

与其为此内容使用 plist,不如说NSUserDefaults是一个更合适的位置。

而不是使用默认的 plist 文件来发送应用程序,而只是registerDefaults:使用NSUserDefaults(通常在您的应用程序委托中完成application:didFinishLaunchingWithOptions:)。

然后,每当进行任何更改时,只需更新NSUserDefaults并调用synchronize以保存更改。

于 2013-06-23T11:00:42.233 回答
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
于 2013-06-23T10:23:20.730 回答