【发布时间】:2023-03-10 19:33:01
【问题描述】:
我正在尝试将每个人的数据保存到我的应用程序中的 plist,但它会将其保存给每个人,因此例如,如果我有一个眼睛颜色字段并且我在 John Doe 的文本字段中输入 Brown,它会保存 Brown对于其他所有人,有没有办法在应用程序中保存每个人的信息,而不是使用 plist?我没有运气尝试:
-(NSString *)pathofFile{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsfolder = [paths objectAtIndex:0];
return [docsfolder stringByAppendingFormat:@"data.plist"];
}
这是我认为加载的代码:
- (void)viewDidLoad {
[self loadPerson];
[super viewDidLoad];
NSString *filepath = [self pathofFile];
if ([[NSFileManager defaultManager]fileExistsAtPath:filepath]) {
NSArray *array = [[NSArray alloc]initWithContentsOfFile:filepath];
Drinfo.text = [array objectAtIndex:0];
[array release];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillTerminate:) name: UIApplicationWillTerminateNotification object:app];
这是我的 saveperson 方法的一部分:
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:self.Drinfo.text];
[array writeToFile:[self pathofFile] atomically:YES];
[array release];
【问题讨论】:
-
一些代码会很有帮助。听起来您在构建数据结构以进入您的 plist 的代码中有一个相当基本的错误,如果我们能看到它,我们可能会很容易地修复它。
标签: ios iphone object save plist