我正在尝试一些超级简单的方法来尝试从 Plist 中保存和加载,但它不起作用。如果有人想查看我的代码并让我知道出了什么问题,我将不胜感激。
- (IBAction)testSave:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistLocation = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistLocation];
[plistDict setObject:@"test" forKey:@"test"];
[plistDict writeToFile:plistLocation atomically: YES];
}
- (IBAction)testLoad:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistLocation = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistLocation];
testField.text = [plistDict objectForKey:@"test"];
}
谢谢!