【原】plist使用的若干问题

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

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

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

NSMutableArray *array = [[NSMutableArray allocinitWithContentsOfFile: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 mainBundlepathForResource:@"Data" ofType:@"plist"];

    NSMutableArray *array = [[NSMutableArray allocinitWithContentsOfFile:path];

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

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

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

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

    [array writeToFile:path atomically:YES];

//[array insertObject:@"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 (*)(ididvoid *))compare context:(void *)context;

- (void)sortUsingSelector:(SEL)comparator;

原文地址:https://www.cnblogs.com/wengzilin/p/2422869.html