ios 中plist文件用于存储数据,我在开发制作小游戏的时候,制作分数排行榜真好用到了plist文件 !在这正好总结一下。我用的plist文件是手动创建的。下面就开始看看吧。
名字是runKu.plist,创建的是空的文件,在代码中看接下来的操作。数据是含有姓名和分数以字典的形式存储在文件中的。
在函数mainViewController.h 中定义变量
- NSMutableDictionary *dic;
- - (void)viewDidLoad
- NSString *str = [[NSBundle mainBundle]pathForResource:@"runKu" ofType:@"plist"];
- dic =[[NSMutableDictionary dictionaryWithContentsOfFile:str] retain];
- NSLog(@"dic is %@",dic);
准备工作做好之后开始对文件进行写的操作了,我是在游戏结束的时候,弹出一个输入姓名的提示框,获取输入的姓名作为字典中的object,游戏结束后的分数作为key值。在字典中是不允许出现重复的数据的,所以我这样的操作还是存在bug的!
- //把信息写入文件
- NSString *name = t1.text;
- NSString *str =[[NSBundle mainBundle]pathForResource:@"runKu" ofType:@"plist"];
- //获取当前时间转化为字符串追加到name后面,记录一下时间
- NSDate *date = [NSDate date];
- NSDateFormatter *df =[[NSDateFormatter alloc]init];
- df.dateFormat = @"yyyyMMddhhmm ";
- NSString *dateStr = [df stringFromDate:date];
- NSString *string1 =[NSString stringWithFormat:@"%d ",score];
- NSString *string =[dateStr stringByAppendingString:name];
- //把数据放进字典中
- [dic setObject:string forKey:string1];
- //写入文件中
- [dic writeToFile:str atomically:YES];
- }
把分数存进文件后,最后输出在UITableView上,即文件的读操作,输出的时候是按照分数的排名的从大到小的。
在输出之前进行了排序操作,分数存进去的时候是以字符串的形式存的,比较大小不是很方便,我用的是按照字符串的长度和大小两方面进行排序的。
只按照的大小的话,默认的是比较第一位,例如123与32进行比较大小的时候,第一位3比1大,只按照大小排序后输出后排序就不正确了。加上长度判断后更准确些。
排序是在scoreViewController.m中的
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // 对文件中的数据进行排序
- NSString *str =[[NSBundle mainBundle]pathForResource:@"runKu" ofType:@"plist"];
- dic =[[NSDictionary dictionaryWithContentsOfFile:str]retain];
- keys = [dic allKeys];
- // 在字典中按照字符串的长度和大小排序,排出来的顺序是从小到大的
- //排序的score是作为字符串比较的大小,根据字符串的长度和大小两部分用代码块进行排序比较
- keys =[[keys sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1,NSString *obj2){
- if ((obj1.length > obj2.length && ([obj1 compare:obj2] ==1 || [obj1 compare:obj2 ]==-1 )) ||(obj2.length ==obj1.length && [obj1 compare:obj2] == 1)){
- return NSOrderedDescending;
- }
- else
- if ((obj1.length<obj2.length && ([obj1 compare:obj2] ==-1 || [obj1 compare:obj2]==-1))|| ((obj2.length==obj1.length) &&[obj1 compare:obj2] == -1))
- return NSOrderedAscending;
- else
- return NSOrderedSame;
- }]retain];
- self.view.backgroundColor =[UIColor blueColor];
- }
- #import <UIKit/UIKit.h>
- @interface scoreViewController : UITableViewController
- {
- NSArray *keys;
- NSDictionary *dic;
- }
- @end
用UITableView还要加入协议等,在这就不在叙述了。
输出到UITableView的函数操作
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
- }
- // 把排好序的字典中的数据倒叙遍历
- int count =[keys count]-indexPath.row-1;
- NSLog(@"row is %d",indexPath.row);
- NSLog(@"count %d",count);
- NSString *date =[dic objectForKey:[keys objectAtIndex:count]];
- // NSString *date =[dic objectForKey:[keys objectAtIndex:indexPath.row]];
- // cell.textLabel.text =[NSString stringWithFormat:@"第 %d 名 : %@",indexPath.row+1,[keys objectAtIndex:indexPath.row]];
- cell.textLabel.text =[NSString stringWithFormat:@"第 %d 名 : %@",indexPath.row+1,[keys objectAtIndex:count]];
- cell.detailTextLabel.text = date;
- return cell;
- }
运行程序就能在模拟器上看见排名了,当然每次重新编译,数据都会被清空的。