打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
2012年3月29日 随笔档案
创建与删除://创建文件管理器NSFileManager *fileManager = [NSFileManager defaultManager];//获取路径//参数NSDocumentDirectory要获取那种路径NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];//去处需要的路径//更改到待操作的目录下[fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];//创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil[fileManager createFileAtPath:@"fileName" contents:nil attributes:nil];//删除待删除的文件[fileManager removeItemAtPath:@"createdNewFile" error:nil];写入数据://获取文件路径NSString *path = [documentsDirectory stringByAppendingPathComponent:@"fileName"];//待写入的数据NSString *temp = @”Hello friend”;int data0 = 100000;float data1 = 23.45f;//创建数据缓冲NSMutableData *writer = [[NSMutableData alloc] init];//将字符串添加到缓冲中[writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];//将其他数据添加到缓冲中[writer appendBytes:&data0 length:sizeof(data0)];[writer appendBytes:&data1 length:sizeof(data1)];//将缓冲的数据写入到文件中[writer writeToFile:path atomically:YES];[writer release];读取数据:int gData0;float gData1;NSString *gData2;NSData *reader = [NSData dataWithContentsOfFile:path];gData2 = [[NSString alloc] initWithData:[reader subdataWithRange:NSMakeRange(0, [temp length])]encoding:NSUTF8StringEncoding];[reader getBytes:&gData0 range:NSMakeRange([temp length], sizeof(gData0))];[reader getBytes:&gData2 range:NSMakeRange([temp length] + sizeof(gData0), sizeof(gData1))];NSLog(@”gData0:%@ gData1:%i gData2:%f”, gData0, gData1, gData2);读取工程中的文件:读取数据时,要看待读取的文件原有的文件格式,是字节码还是文本,我经常需要重文件中读取字节码,所以我写的是读取字节文件的方式。//用于存放数据的变量,因为是字节,所以是UInt8UInt8 b = 0;//获取文件路径NSString *path = [[NSBundle mainBundle] pathForResource:@”fileName” ofType:@”"];//获取数据NSData *reader = [NSData dataWithContentsOfFile:path];//获取字节的个数int length = [reader length];NSLog(@”——->bytesLength:%d”, length);for(int i = 0; i < length; i++) { //读取数据 [reader getBytes:&b range:NSMakeRange(i, sizeof(b))]; NSLog(@”——–>data%d:%d”, i, b);}
posted @ 2012-03-29 10:23 编程小翁 阅读(358) 评论(0) 编辑

弄了半天的plist,最无语的莫过于plist还分种类的。有字典型和数组型等。

plist的写入是,你把你放在工程中的plist删掉。你要写入plist的时候,
如果发现没有该plist,其会帮新建该plist。别傻傻的认为自己建立一个plist,然后
运行程序的时候他会在你建的那plist里面多出几行数据,因为你修改的是应用中的
plist而非你本地的那个plist。
下面的plist里面存放这的是array数组
以下是显示plist的代码:

NSString *path =[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];

NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];

 

 NSLog(@"array:%@",[array objectAtIndex:0]);

写入plist的代码:

 

 NSString *path1 = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];

 NSArray *array1 = [[NSArray alloc] initWithObjects:@"hello1",@"hello2",@"hello3",nil];

 [array1 writeToFile:path1 atomically:YES];

就这么简单。

 

 

  NSString *path =[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];

   NSMutableArray *array= [[NSMutableArray alloc] initWithContentsOfFile:path];

   NSString *str=@"第六章——第三阶——第五页";

   [array insertObject:str atIndex:[array count]];    //添加一行:

   [array removeObjectsAtIndexes:2];                //删除第三行

    [array replaceObjectsAtIndexes:2 withObjects:str;//修改第三行

    [array writeToFile:path atomically:YES];

//[arrayinsertObject:@"hello" atIndex:2];//在第三个数后添加一个hello

//[array removeLastObject];//删掉最后一个

//[array count];         //数组的总数

还有很多函数提供选择:

 

- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes;

- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes;

- (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects;

- (void)addObject:(id)anObject;

- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;

- (void)removeLastObject;

- (void)removeObjectAtIndex:(NSUInteger)index;

- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;

 

- (void)addObjectsFromArray:(NSArray *)otherArray;

- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;

- (void)removeAllObjects;        //清空plist

- (void)removeObject:(id)anObject inRange:(NSRange)range;

- (void)removeObject:(id)anObject;

- (void)removeObjectIdenticalTo:(id)anObject inRange:(NSRange)range;

- (void)removeObjectIdenticalTo:(id)anObject;

- (void)removeObjectsFromIndices:(NSUInteger *)indices numIndices:(NSUInteger)cntNS_DEPRECATED(10_0, 10_6, 2_0, 4_0);

- (void)removeObjectsInArray:(NSArray *)otherArray;

- (void)removeObjectsInRange:(NSRange)range;

- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange;

- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray;

- (void)setArray:(NSArray *)otherArray;

- (void)sortUsingFunction:(NSInteger (*)(id, id, void *))compare context:(void *)context;

- (void)sortUsingSelector:(SEL)comparator;

posted @ 2012-03-29 10:19 编程小翁 阅读(1063) 评论(0) 编辑

保存玩家数据,模拟器读写都可以,而真机plist文件只能读不能写,十分头大,弄球一天找到了问题所在。

按照网上比较有说服力的说法是:iOS程序执行的时候是在“沙盒”里执行。而沙盒里的数据不能写入,只能读取。

经过测试,当一个程序在执行的时候,比如叫 Test.app 的iOS程序,获得他的执行地址的代码是(比如找的是CFG.plist文件)

NSBundle *bundle = [ NSBundle mainBundle ];

NSString *filePath = [ bundle pathForResource:@"CFG" ofType:@"plist" ];

filePath打印出来的执行地址应该类似

Support/iPhone Simulator/5.0/Applications/3B5DBF75-18D2-43EA-B26F-7FEDECAFDC92/Test.app/CFG.plist

每 个应用程序都一个固定且唯一的ID(上面的3B5DBF75-18D2-43EA-B26F-7FEDECAFDC92),这个ID被作为iOS执行时的 一个用来修饰的文件夹,这样可以保证每个应用都是独立的,哪怕名字一样。而这个ID文件夹下有一系列实际存在的文件夹。而Test.app只是其中一个, 里面有实际的游戏数据。如果要想保存数据,那么应该将数据写入到一个叫做“Documents”的文件夹下。访问路径的代码如下:

 NSArray *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docPath = [ doc objectAtIndex:0 ];

执行结果是 Support/iPhone Simulator/5.0/Applications/3B5DBF75-18D2-43EA-B26F-7FEDECAFDC92/Documents

可以看到系统文件名ID和上面的一样。

 

综 上所述。当有数据为只读的时候,应该放到app应用里的plist里,当数据要做修改,应该放到documents里。比如游戏里的物品数据,这种不能被 修改的放到app里,而玩家的合成装备应该在documents里手动创建一个plist来存储。那么首要问题就是要判断,documents里是否已有 数据。

NSArray *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *docPath = [ doc objectAtIndex:0 ];

if( [[NSFileManager defaultManager] fileExistsAtPath:[docPathstringByAppendingPathComponent:@"Score.plist"] ]==NO ) {

   // ============================== 写入plist初始化数据(最后有,先说读取)

}

 

读取:

NSArray *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *docPath = [ doc objectAtIndex:0 ]; // 字典集合。  

NSDictionary *dic = [ NSDictionary dictionaryWithContentsOfFile:[docPathstringByAppendingPathComponent:@"Score.plist"] ]; // 解析数据

NSString *content = [ dic objectForKey:@"Score" ];

NSArray *array = [ content componentsSeparatedByString:@","];

content里就是“Score”里所存储的数据,array是将content里的数据按“,”拆分,仅将两个“,”之间的数据保存。

 

写入:一定要注意,必须创建一个新的NSMutableDictionary

// 用来覆盖原始数据的新dic

NSMutableDictionary *newDic = [ [ NSMutableDictionary alloc ] init ];

// 新数据

NSString *newScore = @"100,200,300";

// 将新的dic里的“Score”项里的数据写为“newScore”

[ newDic setValue:newScore forKey:@"Score" ];

// 将 newDic 保存至docPath+“Score.plist”文件里,也就是覆盖原来的文件

NSArray *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *docPath = [ doc objectAtIndex:0 ];

[ newDic writeToFile:[docPath stringByAppendingPathComponent:@"Score.plist"] atomically:YES ];

posted @ 2012-03-29 10:18 编程小翁 阅读(706) 评论(0) 编辑
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
编程语言学习笔记:ObjC语言 (5) Foundation框架
plist文件
文件管理2
为 NSArray/NSDictionary 优雅地过滤 nil 值 | 吴品诚的技术博客
IOS软件开发揭秘--阅读笔记
2011斯坦福大学iOS应用开发教程学习笔记(第三课)Objective
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服