iphone開發:plist操作iPhone軟體開發教學課程

想在一個類裡把個數組寫進.plist文件裡,再在另一個類裡從這個.plist文件把數組讀取出來?

以name,phoneNumber,age三個字段,為例。我是做的iphone,在文本框中輸入數據,獲取後,裝入數組中。然後把數組寫入.plist文件

寫操作

    NSString *name=[txt1 text];
    NSNumber *phoneNumber=[[NSNumber alloc] initWithInt:[[txt2 text] intValue]];
    NSNumber *age=[[NSNumber alloc] initWithInt:[[txt3 text] intValue]];
   
    NSMutableArray *array=[[NSMutableArray alloc]init];
    [array  addObject:name];
    [array  addObject:phoneNumber];
    [array  addObject:age];

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:@"personal.plist"];   
   
    [array writeToFile:filename  atomically:YES];
    [array release];

讀操作
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:@"personal.plist"];
   
    NSMutableArray *array=[[NSMutableArray alloc] initWithContentsOfFile:filename];
   
    txt1.text=[array objectAtIndex:0];
    txt2.text=[[NSString alloc] initWithFormat:@"%d",[[array objectAtIndex:1] intValue]];
    txt3.text=[[NSString alloc] initWithFormat:@"%d",[[array objectAtIndex:2] intValue]];

    [array release];

 

 

摘自 雲懷空-abel

You May Also Like