成语大全网 - 成语词典 - 如何修改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;

}