Objective c NSArray writeToFile returning“;“已弃用”软件开发包

Objective c NSArray writeToFile returning“;“已弃用”软件开发包,objective-c,ios4,Objective C,Ios4,我试图在应用程序关闭时将一些设置保存到plist文件中,然后在应用程序启动时加载它们。我在一个名为数组的NSArray中保存了4个数字。加载工作正常,因为如果我更改文件,应用程序将以更改文件的方式启动 此代码工作正常: - (void)LoadSettings { NSString *path = [[NSBundle mainBundle] bundlePath]; NSString *finalPath = [path stringByAppendingPathComponent:kSave

我试图在应用程序关闭时将一些设置保存到plist文件中,然后在应用程序启动时加载它们。我在一个名为数组的NSArray中保存了4个数字。加载工作正常,因为如果我更改文件,应用程序将以更改文件的方式启动

此代码工作正常:

- (void)LoadSettings {
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:kSaveFileLocation];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:finalPath];
clockFaceImageSlider.value = [[array objectAtIndex:0] integerValue];
HourTypeSwitch.on = [[array objectAtIndex:1] integerValue];
touchRotationSwitch.on = [[array objectAtIndex:2] integerValue];
accelerometerRotationSwitch.on = [[array objectAtIndex:3] integerValue];
}
此代码适用于保存行:

- (void)SaveSettings {
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *finalPath = [path stringByAppendingPathComponent:kSaveFileLocation];
    NSArray *array = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:clockFaceImageSlider.value], [NSNumber numberWithInt:HourTypeSwitch.on], [NSNumber numberWithInt:touchRotationSwitch.on], [NSNumber numberWithInt:accelerometerRotationSwitch.on], nil];

    if (![array writeToFile:finalPath atomically:YES]) {
        NSLog(@"error");
    }
    //The log does print error
}
有人知道我怎样才能把钱存到穷人手里吗


~Thank

不要写数组的描述NSString,只需写数组即可:

[array writeToFile:finalPath atomically:YES];
或者,如果需要字符串,则应使用en编码类型

NSError *error;
[[array description] writeToFile:finalPath atomically:YES encoding:NSUTF8StringEncodingerror:&error];

但这不会写入plist。

而不是使用
NSArray
本身的
描述(这是
NSString
)并将其写入文件,如中所示

[array writeToFile:finalPath atomically:YES];
NSString
writeToFile:atomicly:
被弃用的原因是它没有正确解释字符编码。谁让你使用
说明
?那人/那本书应该受到惩罚

也就是说,如果只想保存几个条目,只需使用
NSUserDefaults
,就可以更轻松地保存,请参阅。那么你所要做的就是

[[NSUserDefaults standardUserDefaults] setObject: ... forKey:@"..."]

该框架完成了从幕后准备plist路径到将其保存到磁盘的所有工作

谢谢你的快速回复。我不再收到错误,但它不会保存。我修改了我的代码以检查它是否保存,并返回错误消息。我已经更新了我的问题。你必须在你的应用程序的文档目录中写入你的文件。那是你唯一允许的地方。NSString*documentsDirectory=[NSHomeDirectory()stringByAppendingPathComponent:@“Documents”];是您要保存资料的文件夹。