當前位置:成語大全網 - 成語詞典 - 如何修改plist文件

如何修改plist文件

二.創建壹個保存學生信息的類 SaveStudentMessagePlist 並繼承 NSObject

SaveStudentMessagePlist.h 文件裏創建壹個初始化的函數

#import <Foundation/Foundation.h>

@interface SaveStudentMessagePlist : NSObject

-(id)initWithStudentName:(NSString *)name Studentage:(NSString *)age StudentNumber:(NSString * )numerb StudentNation:(NSString *)nation;

@end

//////////////////////////////////////////在.m文件裏實現方法///////////////////////////////////////////////////////////////////

-(id)initWithStudentName:(NSString *)name Studentage:(NSString *)age StudentNumber:(NSString * )numerb StudentNation:(NSString *)nation

{

//我們把學生的信息保存在可變數組裏

NSMutableArray *studentMessageArray = [[NSMutableArray alloc]initWithObjects:name,age,numerb,nation,nil];

if (self = [superinit])

{

[self createStudentMessagePlist:studentMessageArraykey:numerb]; // 因為學生的學號是唯壹的作為詞典的key值

}

return self;

}

// 創建plist 寫入操作

-(void)createStudentMessagePlist:(NSMutableArray *)studentMessageArray key:(NSString *)studentNumebr

{

//沙盒路徑

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

// plist 路徑

NSString *plistPath = [documentsDirectorystringByAppendingPathComponent:@"student.plist"];

NSFileManager *fileManager = [[NSFileManager alloc]init];

// 下面這幾步很重要 通過文件管理器 來判讀plist 文件是否存在! 如果不存在 我們就通過 [fileManager createFileAtPath:plistPath contents:nil attributes:nil創建壹個plist 並檢測是否成功失敗!存在後寫入詞典

如何存在plist 我們就 在 studentMessageDic 可變詞典裏保存在來色數據這樣可以避免數據被覆蓋問題

if(![fileManager fileExistsAtPath:plistPath])

{

if(![fileManager createFileAtPath:plistPathcontents:nilattributes:nil])

{

NSLog(@"create file error");

}

else

{

NSDictionary* studentMessageDic = [NSDictionary dictionaryWithObjectsAndKeys:studentMessageArray,studentNumebr ,nil];

[studentMessageDic writeToFile:plistPathatomically:YES];

}

}

else

{

NSMutableDictionary *studentMessageDic = [[NSMutableDictionary alloc]initWithContentsOfFile: plistPath];

[studentMessageDic setObject: studentMessageArray forKey:studentNumebr ];

[studentMessageDic writeToFile:plistPathatomically:YES];

}

}

現在讓我們調用方法吧! 打印路徑plistPath 我們在沙盒裏就會找到妳的文件了如下圖

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

if (![studentNumberTextField.tex tisEqualToString:@"" ])

{

SaveStudentMessagePlist *save = [[SaveStudentMessagePlist alloc]initWithStudentName:studentNameTextField.textStudentage:studentAgeTextFIeld.textStudentNumber:studentNumberTextField.textStudentNation:studentnationTextField.text];

}

}

三 對plist 經行讀操作

我創建了壹個只定義的tableview 來顯示學生的信息

-(void)readStudentMessageFromPlist

{

//創建文件管理器

NSFileManager *fileManager = [NSFileManager defaultManager];

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *documentation = [path objectAtIndex:0];

//更改到待操作的目錄下

[fileManager changeCurrentDirectoryPath:[documentationstringByExpandingTildeInPath]];

NSString *studentPlistPath = [documentation stringByAppendingPathComponent:@"student.plist"];

_studentMessageDic = [[NSMutableDictionary alloc]initWithContentsOfFile:studentPlistPath];

NSLog(@"%d", [_studentMessageDicallKeys].count);

}

然後在tableview上顯示

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

// Return the number of sections.

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

// Return the number of rows in the section.

return [[_studentMessageDicallKeys]count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

NSString *keyValue = [[_studentMessageDic allKeys]objectAtIndex:indexPath.row];

NSArray *studenMessageArr = [_studentMessageDic objectForKey:keyValue];

static NSString *CellIdentifier =@"Cell";

StudentCell *cell = (StudentCell *)[tableView dequeueReusableCellWithIdentifier:@"StudentCell"];

if (cell == nil)

{

if (cell == nil)

{

cell = [[StudentCellalloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}

}

cell.name.text = [studenMessageArrobjectAtIndex:0];

cell.age.text = [studenMessageArrobjectAtIndex:1];

cell.number.text = [studenMessageArrobjectAtIndex:2];

cell.national.text = [studenMessageArrobjectAtIndex:3];

return cell;

}