代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
ObjC
】
plist基本操作
作者:
茶壶
/ 发布于
2015/6/29
/
1228
// // UserPlistService.h // HTest_Plist // // Created by 胡晓阳 on 15/6/25. // Copyright (c) 2015年 HXY. All rights reserved. // #import <Foundation/Foundation.h> @interface UserPlistService : NSObject +(UserPlistService *)share; /** * 获取所有数据 * * @param success 成功 * @param error 失败 */ -(void)fetchAllUserPlistSuccess:(void(^)(id successObj))success error:(void(^)(void))error; /** * 添加一条记录 * * @param name 姓名 * @param score 成绩 * @param success 成功 * @param error 失败 */ -(void)insertWithName:(NSString *)name score:(NSString *)score scucess:(void(^)(id successObj))success error:(void(^)(void))error; /** * 删除一条记录 * * @param name 姓名 * @param score 成绩 * @param success 成功 * @param error 失败 */ -(void)deleteWithName:(NSString *)name score:(NSString *)score success:(void(^)(id successObj))success error:(void(^)(void))error; /** * 更新一条数据 * * @param name <#name description#> * @param score <#score description#> * @param success <#success description#> * @param error <#error description#> */ -(void)updateWithName:(NSString *)name score:(NSString *)score atIndex:(NSInteger)index success:(void(^)(id successObj))success error:(void(^)(void))error; -(void)sortWithType:(NSInteger)type success:(void(^)(id successObj))success error:(void(^)(void))eror; @end // // UserPlistService.m // HTest_Plist // // Created by 胡晓阳 on 15/6/25. // Copyright (c) 2015年 HXY. All rights reserved. // #import "UserPlistService.h" #define UserPlistName @"user.plist" @implementation UserPlistService +(UserPlistService *)share { static UserPlistService *_service = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _service = [[UserPlistService alloc] init]; }); return _service; } /** * 获取所有数据 * * @param success 成功 * @param error 失败 */ -(void)fetchAllUserPlistSuccess:(void (^)(id successObj))success error:(void (^)(void))error { NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *plistPath = [documentDirectory stringByAppendingPathComponent:UserPlistName]; if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) { NSMutableArray *userPlist = [NSMutableArray arrayWithContentsOfFile:plistPath]; success(userPlist); }else { NSMutableArray *userList = [[NSMutableArray alloc] init]; [userList writeToFile:plistPath atomically:YES]; error(); } } /** * 添加一条记录 * * @param name 姓名 * @param score 成绩 * @param success 成功 * @param error 失败 */ -(void)insertWithName:(NSString *)name score:(NSString *)score scucess:(void (^)(id success))success error:(void (^)(void))error { NSString *plistPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:UserPlistName]; NSMutableArray *userList = [[NSMutableArray alloc] initWithContentsOfFile:plistPath]; NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:name,@"name",score,@"score", nil]; [userList addObject:dic]; [userList writeToFile:plistPath atomically:YES]; success(userList); } /** * 删除一条记录 * * @param name 姓名 * @param score 成绩 * @param success 成功 * @param error 失败 */ -(void)deleteWithName:(NSString *)name score:(NSString *)score success:(void (^)(id))success error:(void (^)(void))error { NSString *plistPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:UserPlistName]; NSMutableArray *userList = [NSMutableArray arrayWithContentsOfFile:plistPath]; [userList removeObject:@{@"name":name,@"score":score}]; [userList writeToFile:plistPath atomically:YES]; success(userList); } /** * 更新一条数据 * * @param name <#name description#> * @param score <#score description#> * @param success <#success description#> * @param error <#error description#> */ -(void)updateWithName:(NSString *)name score:(NSString *)score atIndex:(NSInteger)index success:(void (^)(id))success error:(void (^)(void))error { NSString *plistPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:UserPlistName]; NSMutableArray *userList = [[NSMutableArray alloc] initWithContentsOfFile:plistPath]; NSDictionary *newDic = @{@"name":name,@"score":score}; [userList replaceObjectAtIndex:index withObject:newDic]; if ([userList writeToFile:plistPath atomically:YES]) { success(userList); }else { NSLog(@"写文件失败"); } } /** * 给记录排序 * * @param type 0:升序 1:降序 * @param success 成功 * @param eror 失败 */ -(void)sortWithType:(NSInteger)type success:(void (^)(id successObj))success error:(void (^)(void))eror { NSString *plistPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:UserPlistName]; NSMutableArray *userList = [NSMutableArray arrayWithContentsOfFile:plistPath]; if (type == 0) { //升序 NSSortDescriptor *scoreUp_sort = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:YES]; NSArray *arr = [userList sortedArrayUsingDescriptors:@[scoreUp_sort]]; if ([arr writeToFile:plistPath atomically:YES]) { success(arr); } }else { //降序 NSSortDescriptor *scoreDown_sort = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:NO]; NSArray *arr = [userList sortedArrayUsingDescriptors:@[scoreDown_sort]]; if ([arr writeToFile:plistPath atomically:YES]) { success(arr); } } } @end
试试其它关键字
plist
同语言下
.
根据生日计算星座
.
精简的实现一个内存池
.
iOS索引搜索核心代码
.
iOS 搜索框
.
文件操作
.
UIWebView 加载本地网页
.
取消导航条对视图的影响
.
利用终端显示 隐藏文件
.
更改导航条背景颜色
.
iOS 字体斜体
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
茶壶
贡献的其它代码
(
9
)
.
毛玻璃效果
.
整数N随机分成n份
.
如何实现获取当前定位信息
.
获取相册的所有相片
.
plist基本操作
.
获取客户端真实ip地址
.
仿async的 并行执行和序列执行函数
.
二元搜索
.
图的遍历
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3