views:

61

answers:

1

hi guys, i am trying to save data to the plist in my app per person, but it save it to everyone, so for example if i have a field for eye color and i enter brown in the textfield for john doe, it save brown for everyone else as well, is there a way to save this info per person in the app instead using plist, i have had no luck trying

thanks

-(NSString *)pathofFile{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsfolder = [paths objectAtIndex:0];
    return [docsfolder stringByAppendingFormat:@"data.plist"];
}

here is the code i have in my view did load:

- (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];

and here is what i have as part of my saveperson method:

NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:self.Drinfo.text];
[array writeToFile:[self pathofFile] atomically:YES];
[array release];
A: 

You could use a plist just create a NSDictionary for each person inside the pList and add the colour string to that. The steps would be something like

  1. Open up the pList from file, set it to be a NSDictionary and make sure its a mutable copy.
  2. Create a new NSMutableDictionary and add any attributes to that e.g. eye colour.
  3. Add that mutable dictionary to your pList dictionary with the key set to the persons name
  4. Save the pList
octermircty