2010-08-08 120 views
0

我试图将数据保存到每个人的应用程序中的plist,但它将其保存到每个人,所以例如,如果我有一个眼睛的颜色领域,我在textfield for John Doe,它为其他人保存了Brown,有没有办法在应用程序中保存这些信息,而不是使用plist?我有没有运气尝试:将数据保存到每个对象或个人的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]; 
+0

一些代码将是很有益的。这听起来像是代码中存在一个相当基本的错误,它会构建数据结构以进入plist,如果我们能够看到它,我们可以很容易地修复它。 – David 2010-08-08 01:54:54

+0

thx大卫,我发布的代码 – adea 2010-08-08 20:15:29

回答

0

您可以使用plist,为plist中的每个人创建一个NSDictionary,然后将颜色字符串添加到该字符串中。具体的步骤是这样的

  1. 从文件打开的plist,将其设置为一个NSDictionary,并确保它的一个mutable copy
  2. 创建一个新的NSMutableDictionary并为该例添加任何属性。眼睛的颜色。
  3. 添加一个mutable dictionaryplistdictionary与按键的人的名字
  4. 保存plist
相关问题