iOS 개발 데이터 저장소plist

1425 단어
      .....
  
-(void) writeData
{
    //       
    NSArray * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentPath = path[0];
    NSString * plistPath = [documentPath stringByAppendingPathComponent:@"Data.plist"];
    NSDictionary * dic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];    

    NSMutableArray * dataArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < 6; i ++) {
        [dataArray addObject:@{@"name":[NSString stringWithFormat:@" %d",i],@"age":@(i + 20),@"adress":[NSString stringWithFormat:@"xx %d ",i+2]}];
    }
    
    dic = @{@"userData":dataArray};
    
    [dic writeToFile:plistPath atomically:YES];
}

//  
-(void) readData
{
    //       
    NSArray * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentPath = path[0];
    NSString * plistPath = [documentPath stringByAppendingPathComponent:@"Data.plist"];
    NSDictionary * dic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    
    //           ,    ,    
//    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Datas" ofType:@"plist"];
//    NSDictionary * dic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    
    NSLog(@"root = %@",dic);
}

좋은 웹페이지 즐겨찾기