[基础总结]关于plist

来源:互联网 发布:淘宝开店贷款 编辑:程序博客网 时间:2024/05/22 03:45

前面提到了touch,但是touch一般只能读不能写,这样是不能存储游戏中玩家的数据的,所以绝大部分iphone和手机应用都使用到了plist.plist是可以存取少量的数据的。

             由于xcode本身支持和携带plist,所以使用起来,也是蛮方便的。甚至不需要专门引用头文件。而取出方式和json是一样的,也是用数组和指针就行了,而它的存储方式,其实和取出方式也差不多的。具体的函数如下:

           存储:

 NSString *name = [NSStringstringWithFormat:@"%d",i];          

        NSMutableArray *array=[[NSMutableArrayalloc]init];

        [array  addObject:name];

               

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

        NSString *path=[paths    objectAtIndex:0];

        NSString *filename=[path stringByAppendingPathComponent:@"personal.plist"];    

        

        [array writeToFile:filename  atomically:YES];

        [array release];   

 
        取出:
        


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

    NSString *path=[paths    objectAtIndex:0];

    NSString *filename=[pathstringByAppendingPathComponent:@"personal.plist"];

    

    NSMutableArray *array=[[NSMutableArrayalloc] initWithContentsOfFile:filename];

    

    NSString *text=[[NSStringalloc] initWithFormat:@"%d",[[arrayobjectAtIndex:0]intValue]];   

               
 
         上面的name是要存储的数据,下面的2行代码是为它创建存储空间,然后paths path filename都是连接plist的代码,
       存取的时候你的第一个数据记为objectAtIndex:0,依次类推,如果继续存储即为objectAtIndex:objectAtIndex:2.。。
   取出的时候也是同理,根据你想取出的数据而分别调出objectAtIndex:0或objectAtIndex:1.
    plist大概的使用方法就是这样,但plist也只能存取少量的数据,将来写道具和技能系统要存取大量数据应该怎么办,到时候再研究吧。
原创粉丝点击