1、
NSArray *array = [[NSArray arrayWithObjects:@"Apple",@"Google",@"Microsoft",nil];
[array writeToFile:@"plist.txt" atomically:NO];
会在当前文件夹下找到plist.txt,格式如下
(
Apple,
Google,
Microsoft
)
NSArray *array2 = [NSArray arrayWithContentOfFile:@"plist.txt"];
NSLog(@"%@",[array2 objectAtIndex:1]);
2、如果持久化的类型不是数组,字典,缓冲区,那该怎么办。
objc里也有java一样的序列化,反序列化 使用NSCoding协议
-(void) encodeWithCoder :(NSCoder*) coder; //编码对象
-(id) initWithCoder :(NSCoder*) decoder; //解码
@interface Person : NSObject <NSCoding>
{
int pid;
NSString *name;
float height;
}
-(void) setPid:(int) pid;
-(void) setName:(NSString *) name;
-(void) setHeight:(float) height;
-(int) pid;
-(NSString *) name;
-(float) height;
@end
#import "Person.h"
@implementation Person
-(void) setPid:(int) p
{
pid = p;
}
-(void) setName:(NSString *) n
{
[n retain];
[name release];
name = n;
}
-(void) setHeight:(float) h
{
height = h;
}
-(int) pid
{
return pid;
}
-(NSString *) name
{
return name;
}
-(float) height
{
return height;
}
-(void) encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:name forKey:@"name"];
[aCoder encodeInt:pid forKey:@"pid"];
[aCoder encodeFloat:height forKey:@"height"];
}
- (id)initWithCoder:(NSCoder *)coder {
self = [super init];
if (self) {
name = [coder decodeObjectForKey:@"name"];
[self setName:[coder decodeObjectForKey:@"name"]];
pid = [coder decodeIntForKey:@"pid"];
height = [coder decodeFloatForKey:@"height"];
}
return self;
}
- (void)dealloc {
[name release];
[super dealloc];
}
@endint main (int argc, const char * argv[])
{
@autoreleasepool {
Person *p = [[Person alloc] init];
[p setName:@"@Tom"];
[p setPid:1];
[p setHeight:188];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:p];
[data writeToFile:@"person.db" atomically:YES];
[p release];
Person *p2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"%@",[p2 name]);
3、对象的复制
某个对象是否可以被复制,要看它的类型是否遵从NSCopying协议,这个协议有个复制方法需要我们去实现
//Engine.h=======================================
#import <Foundation/Foundation.h>
@interface Engine:NSObject <NSCopying>//遵从NSCopying协议
@end
//Engine.m=======================================
@implementation Engine
-(id) copyWithZone:(NSZone*) zone{
Engine *engine = [[[self class] allocWithZone:zone] init];
return engine;
}
@end
//=======================================
@interface Tire:NSObject <NSCopying>{
int count;
}
-(Tire*) initWithCount:(int) count;
-(void) setCount:(int) count;
-(int) count;
@end
//=======================================
@implementation Tire
-(Tire*) initWithCount:(int) c{
self = [super init];
if(self){count=c;}
return self;
}
-(void) setCount:(int) c{
count = c;
}
-(int) count{
return count;
}
-(id) copyWithZone:(NSZone*) zone{
Tire *tire = [[[self class] allocWithZone:zone] initWithCount:count];
return tire;
}
@end
//========================================
@interface Car : NSObject <NSCopying>{
Engine *engine;Tire *tire;
}
-(void) setEngine:(Engine*) engine;
-(void) setTire:(Tire*) tire;
-(Engine*) engine;
-(Tire*) tire;
@end
//========================================
@implementation Car
-(void) setEngine:(Engine*) e{
[e retain];
[engine release];
engine = e;
}
-(void) setTire:(Tire*) t{
[t retain];
[tire release];
tire = t;
}
-(Engine*) engine{
return engine;
}
-(Tire*) tire{
return tire;
}
-(id) copyWithZone:(NSZone*) zone{
Car *car = [[[self class] allocWithZone:zone] init];
Engine *engineCopy = [engine copy];
Tire *tireCopy = [tire copy];
[car setEngine:engineCopy];
[car setTire:tireCopy];
[engineCopy release];
[tireCopy release];
return car;
}
-(void) dealloc{
[engine release];
[tire release];
[super dealloc];
}
@end
int main (int argc, const char * argv[]){
@autoreleasepool {
Engine *engine = [Engine new];
Tire *tire = [[Tire alloc] initWithCount:4];
Car *car = [Car new];
[car setEngine:engine];
[car setTire:tire];
[engine release];
[tire release];
Car *newCar = [car copy];
[car release];
NSLog(@"The car has %d tires",[[newCar tire] count]);
newCar release];
}
return 0;
}
本文介绍了 Objective-C 中数组的文件持久化方法,演示了如何使用 NSCoding 协议进行对象的序列化与反序列化,并展示了如何通过实现 NSCopying 协议来完成对象的深复制。

被折叠的 条评论
为什么被折叠?



