.plist 的读写

ios 中plist文件用于存储数据,我在开发制作小游戏的时候,制作分数排行榜真好用到了plist文件 !在这正好总结一下。我用的plist文件是手动创建的。下面就开始看看吧。


名字是runKu.plist,创建的是空的文件,在代码中看接下来的操作。数据是含有姓名和分数以字典的形式存储在文件中的。

在函数mainViewController.h 中定义变量

[html]  view plain copy
  1. NSMutableDictionary *dic;  
mainViewController.m中的
[html]  view plain copy
  1. - (void)viewDidLoad  
函数中做好准备工作:

[html]  view plain copy
  1. NSString *str = [[NSBundle  mainBundle]pathForResource:@"runKu" ofType:@"plist"];  
  2.     dic =[[NSMutableDictionary dictionaryWithContentsOfFile:str] retain];  
  3.     NSLog(@"dic is %@",dic);  
在不同的地方加入输出语句,个人感觉很清晰观察到运行的结果,有利于调试!

准备工作做好之后开始对文件进行写的操作了,我是在游戏结束的时候,弹出一个输入姓名的提示框,获取输入的姓名作为字典中的object,游戏结束后的分数作为key值。在字典中是不允许出现重复的数据的,所以我这样的操作还是存在bug的!

[html]  view plain copy
  1. //把信息写入文件  
  2.     NSString *name = t1.text;  
  3.     NSString *str =[[NSBundle mainBundle]pathForResource:@"runKu" ofType:@"plist"];  
  4.     //获取当前时间转化为字符串追加到name后面,记录一下时间  
  5.     NSDate *date = [NSDate date];  
  6.     NSDateFormatter *df =[[NSDateFormatter alloc]init];  
  7.     df.dateFormat = @"yyyyMMddhhmm  ";  
  8.     NSString *dateStr = [df stringFromDate:date];  
  9.     NSString *string1 =[NSString stringWithFormat:@"%d  ",score];  
  10.     NSString *string =[dateStr stringByAppendingString:name];  
  11.      
  12.     //把数据放进字典中  
  13.     [dic setObject:string forKey:string1];  
  14.     //写入文件中  
  15.     [dic writeToFile:str atomically:YES];  
  16.     }  
  17.      
获取当时时间追加到字符串name后面,减小了bug的出现。

把分数存进文件后,最后输出在UITableView上,即文件的读操作,输出的时候是按照分数的排名的从大到小的。

在输出之前进行了排序操作,分数存进去的时候是以字符串的形式存的,比较大小不是很方便,我用的是按照字符串的长度和大小两方面进行排序的。

只按照的大小的话,默认的是比较第一位,例如123与32进行比较大小的时候,第一位3比1大,只按照大小排序后输出后排序就不正确了。加上长度判断后更准确些。

排序是在scoreViewController.m中的

[html]  view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.   
  5. //    对文件中的数据进行排序  
  6.       
  7.     NSString *str =[[NSBundle mainBundle]pathForResource:@"runKu" ofType:@"plist"];  
  8.     dic =[[NSDictionary dictionaryWithContentsOfFile:str]retain];  
  9.     keys = [dic allKeys];  
  10. //      在字典中按照字符串的长度和大小排序,排出来的顺序是从小到大的  
  11. //排序的score是作为字符串比较的大小,根据字符串的长度和大小两部分用代码块进行排序比较  
  12.       
  13.     keys =[[keys sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1,NSString *obj2){  
  14.           
  15.         if ((obj1.length > obj2.length && ([obj1 compare:obj2] ==1 || [obj1 compare:obj2 ]==-1 )) ||(obj2.length ==obj1.length && [obj1 compare:obj2] == 1)){  
  16.                 return NSOrderedDescending;  
  17.         }  
  18.         else  
  19.             if ((obj1.length<obj2.length && ([obj1 compare:obj2] ==-1 || [obj1 compare:obj2]==-1))|| ((obj2.length==obj1.length) &&[obj1 compare:obj2] == -1))  
  20.                 return NSOrderedAscending;  
  21.         else  
  22.                 return NSOrderedSame;  
  23.   
  24.           
  25.     }]retain];  
  26.   
  27.     self.view.backgroundColor =[UIColor blueColor];  
  28.       
  29. }  
keys是在scoreViewController.h中定义的

[html]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface scoreViewController : UITableViewController  
  4. {  
  5.     NSArray *keys;  
  6.     NSDictionary *dic;  
  7. }  
  8. @end  

用UITableView还要加入协议等,在这就不在叙述了。

输出到UITableView的函数操作

[html]  view plain copy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.      
  4.     static NSString *CellIdentifier = @"Cell";  
  5.     UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  6.     if (cell == nil) {  
  7.         cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];  
  8.     }  
  9. //    把排好序的字典中的数据倒叙遍历  
  10.       
  11.     int count =[keys count]-indexPath.row-1;  
  12.     NSLog(@"row is %d",indexPath.row);  
  13.     NSLog(@"count %d",count);  
  14.       
  15.     NSString *date =[dic  objectForKey:[keys objectAtIndex:count]];  
  16. //    NSString *date =[dic  objectForKey:[keys objectAtIndex:indexPath.row]];  
  17. //    cell.textLabel.text =[NSString stringWithFormat:@"第 %d 名 :  %@",indexPath.row+1,[keys objectAtIndex:indexPath.row]];  
  18.     cell.textLabel.text =[NSString stringWithFormat:@"第 %d 名 :  %@",indexPath.row+1,[keys objectAtIndex:count]];  
  19.     cell.detailTextLabel.text = date;  
  20.      
  21.     return cell;  
  22. }  

运行程序就能在模拟器上看见排名了,当然每次重新编译,数据都会被清空的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值