在APP里面,Plist文件常用于數據存儲!
- A.應用包里的“plist文件”
NSString * path = [[NSBundle mainBundle]pathForResource:@"RankList.plist" ofType:nil];
NSArray * plistArray = [[NSArray alloc] initWithContentsOfFile:path];
for (NSDictionary * dict in plistArray) {
[self.dataArray addObject:dict];
}
創建:
plist文件的創建
注意:應用包里的plist文件 只能讀取數據!不能寫入數據!
[NSBundle mainBundle]
獲取到的路徑下的文件 不能修改NSString * path = [[NSBundle mainBundle] pathForResource:@"gg.plist" ofType:nil]; NSDictionary * plistDict = [[NSDictionary alloc] initWithContentsOfFile:path]; NSLog(@"plistDict:%@",plistDict); NSDictionary * addDict = @{ @"score":@"123", @"name":@"adama" }; if ([addDict writeToFile:path atomically:YES]) { NSLog(@"將數組保存為屬性列表文件成功!!"); NSDictionary * plistDict = [[NSDictionary alloc] initWithContentsOfFile:path]; NSLog(@"plistDict:%@",plistDict); }else{ NSLog(@"將數組保存為屬性列表文件不成功"); }
2017-08-26 10:03:31.734 plistTest[1604:54082] plistDict:{ } 2017-08-26 10:03:31.735 plistTest[1604:54082] 將數組保存為屬性列表文件成功!! 2017-08-26 10:03:31.735 plistTest[1604:54082] plistDict:{ name = ada; score = da; }
展示存儲成功!但并非有效!(每次運行都一樣)
plist文件始終不變:plist文件 始終為空
- B.沙盒中的“plist文件”
NSArray *sandboxpath= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //獲取完整沙盒路徑
NSString *documentsDirectory = [sandboxpath objectAtIndex:0];//documents路徑
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"RankList.plist"];
NSArray * plistDataArray = [[NSArray alloc] initWithContentsOfFile:plistPath];
for (NSDictionary * dict in plistDataArray) {
[self.dataArray addObject:dict];
}
排行榜實現
由于刪除、添加、修改數據(考慮到是否存在Plist文件?),故而Plist文件選擇在沙盒中創建、使用:
全局變量:
NSString * _jifenStr; //積分數
NSString * _nameStr; //用戶名
int _indexWhereAdd; //插入的位置
操作步驟:
NSArray *sandboxpath= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //獲取完整路徑
NSString *documentsDirectory = [sandboxpath objectAtIndex:0];//沙盒document路徑,放著plist文件
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"RankList.plist"];//plist文件路徑
BOOL hasTheRightPlist = NO; //文檔路徑下 是否含有plist文件
NSFileManager * fileManger = [NSFileManager defaultManager]; //文件管理類
NSArray * dirArray = [fileManger contentsOfDirectoryAtPath:documentsDirectory error:nil];
for (NSString * str in dirArray) { //遍歷文件夾(沙盒document路徑)
if ([str isEqualToString:@"RankList.plist"]) { //含有“RankList.plist”
hasTheRightPlist = YES;
}
}
if (hasTheRightPlist == YES) { //沙盒document路徑 含有“RankList.plist”文件
NSArray * plistArray = [[NSArray alloc] initWithContentsOfFile:plistPath];
for (int i = 0; i < plistArray.count; i++) {
NSDictionary * dict = plistArray[i];
//比較 歷史積分、當前積分 進行排行
if([_jifenStr integerValue] > [dict[@"score"] integerValue] ) {//大于 直接插入
UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"提交分數" message:@"恭喜進入排行榜" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確認", nil];
[alertV setAlertViewStyle:UIAlertViewStylePlainTextInput];
UITextField * textName = [alertV textFieldAtIndex:0];
textName.placeholder = @"請輸入名字";
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
[alertV show];
alertV.tag = 0;
_indexWhereAdd = i;
break;
} else { //小于
if (i == plistArray.count-1 && plistArray.count < 10) { //最后一位 && 個數沒超過排行榜限制
UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"提交分數" message:@"恭喜進入排行榜" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確認", nil];
[alertV setAlertViewStyle:UIAlertViewStylePlainTextInput];
UITextField * textName = [alertV textFieldAtIndex:0];
textName.placeholder = @"請輸入名字";
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
[alertV show];
alertV.tag = 1;
_indexWhereAdd = i;
break;
}
}
}
} else {
UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"提交分數" message:@"恭喜進入排行榜" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確認", nil];
[alertV setAlertViewStyle:UIAlertViewStylePlainTextInput];
UITextField * textName = [alertV textFieldAtIndex:0];
textName.placeholder = @"請輸入名字";
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
[alertV show];
alertV.tag = 2;
//文件夾下,不存在plist文件 直接添加,必定要創建plist文件
}
#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
UITextField * textNameTF = [alertView textFieldAtIndex:0];
_nameStr = textNameTF.text; //用戶名(輸入框)
if (buttonIndex == 0 && _nameStr.length > 0) { //確認按鈕
NSArray *sandboxpath= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //獲取完整路徑
NSString *documentsDirectory = [sandboxpath objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"RankList.plist"];
NSArray * plistArray = [[NSArray alloc] initWithContentsOfFile:plistPath];
NSMutableArray * addToPlistArr; //存儲的數組
if (plistArray) {
addToPlistArr = plistArray.mutableCopy;
} else { //plist數組 不存在
addToPlistArr = @[].mutableCopy;
}
NSDictionary * addDict = @{
@"score":_jifenStr,
@"name":_nameStr
};
switch (alertView.tag) {
case 0:{
[addToPlistArr insertObject:addDict atIndex:_indexWhereAdd];
if (addToPlistArr.count > 10) { //超過10位,移除最后一位
[addToPlistArr removeObjectAtIndex:(addToPlistArr.count-1)];
}
if ([addToPlistArr writeToFile:plistPath atomically:YES]) {
NSLog(@"將數組保存為屬性列表文件成功!!");
}else{
NSLog(@"將數組保存為屬性列表文件不成功");
}
}break;
case 1:{
[addToPlistArr addObject:addDict];
if ([addToPlistArr writeToFile:plistPath atomically:YES]) {
NSLog(@"將數組保存為屬性列表文件成功!!");
}else{
NSLog(@"將數組保存為屬性列表文件不成功");
}
}break;
case 2:{
//文件夾下,不存在plist文件 ??????直接添加,會先創建plist文件??????
[addToPlistArr addObject:addDict];
if ([addToPlistArr writeToFile:plistPath atomically:YES]) {
NSLog(@"將數組保存為屬性列表文件成功!!");
}else{
NSLog(@"將數組保存為屬性列表文件不成功");
}
}break;
default:
break;
}
} else {
//AlertView里沒有輸入任何信息
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.label.text = @"選擇默默無聞,分數不保存";
hud.margin = 10.f;
hud.offset = CGPointMake(0, -60.f);
hud.removeFromSuperViewOnHide = YES;
[hud hideAnimated:YES afterDelay:1.5f];
}
//NSLog(@"%ld",buttonIndex);
}
邏輯判斷
判斷文件夾路徑下是否有“RankList.plist”文件:
不包含:
創建“RankList.plist”文件(數組:Array),并添加一個對象信息(字典:Dictionary)-
包含:存儲的數據 排序(積分:_jifenStr)
-
大于 “RankList.plist”文件(數組:Array)中任一元素(對象信息)
數據(Dictionary):
直接插入相應的位置。若插入數據后,若數組個數超過限制(10個):需要移除最后一個元素。
-
不大于 “RankList.plist”文件(數組:Array)中任一元素(對象信息)
數據(Dictionary):數組個數 超過限制(10個):不做處理
數組個數 不超過限制(10個):直接添加到數組最后面
-
goyohol's essay