【原】plist使用的若干问题
弄了半天的plist,最无语的莫过于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];
//[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 (*)(id, id, void *))compare context:(void *)context;
- (void)sortUsingSelector:(SEL)comparator;