使用NSObjects帮助将文件保存到文档NSMutableArray
我是一个新手iOS开发人员。 我编写了一个小型应用程序,使用从NSObject派生的对象保存NSMutableArray数组。 应用程序执行保存但文件不在文档目录中创建,应用程序无法读取。 这个问题是在模拟器和我的iPhone 3gs 4.2.1
appDelegate类中的我的NSMutableArray定义:
@property (nonatomic,retain, readwrite) NSMutableArray *places;
我的NSObject类:
#import <Foundation/Foundation.h>
@interface地方:NSObject {NSString * name; NSString *位置; }
- (id)init:(NSString *)name:(NSString *)location;
@property(retain,nonatomic,readwrite)NSString * name; @property(retain,nonatomic,readwrite)NSString * location;
@结束
我的StorageService库类:
#import "StorageService.h"
@implementation StorageService
- (id)init {self = [super init]; if(self!= nil){
}
return self;
}
- (void)saveArrayToFile:(NSString *)filename:(NSMutableArray *)arrayToSave {//获取完整路径NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString * fullPath = [paths objectAtIndex:0]; fullPath = [fullPath stringByAppendingPathComponent:filename];
NSLog(@“保存在%@”,fullPath); [arrayToSave writeToFile:完整路径原子:是]; }
- (NSMutableArray *)readArrayFromFile:(NSString *)filename {//获取完整路径NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString * fullPath = [paths objectAtIndex:0]; fullPath = [fullPath stringByAppendingPathComponent:filename];
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {
NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:fullPath];
if (data == nil) {
data = [[NSMutableArray alloc] init];
}
NSLog(@"Read from %@",fullPath);
return data;
} else {
NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:fullPath];
return data;
}
}
- (void)dealloc {[super dealloc]; }
@结束
和我的函数在AppDelegate中:
-(void) saveApplicationData {
[self.storageService saveArrayToFile : PLACES_FILE : self.places];
}
- (void)loadApplicationData {self.places = [self.storageService readArrayFromFile:PLACES_FILE]; }
这是我的类保持不变的文件名:
#import <Foundation/Foundation.h>
extern NSString * const PLACES_FILE = @“Places.dat”;
@interface ApplicationConstants:NSObject {
}
@结束
那么,什么是错的?
感谢你们。
你想要的是让Place
符合NSCoding
协议,以允许序列化到文件和从文件(如果需要的话在内存数据中)
扩展Place
为(我也改变了init方法的名称,您的姓名是对每一个命名惯例内部监督办公室):
@interface Place : NSObject <NSCoding> {
NSString *name;
NSString *location;
}
-(id)initWithName:(NSString *)name location:(NSString *)location;
@property (retain,nonatomic,readwrite) NSString *name;
@property (retain,nonatomic,readwrite) NSString *location;
@end
您的实现非常简单,但您还需要实现由NSCoding
协议定义的两个方法:
@implementation Place
@synthesize name, location;
-(id)initWithName:(NSString *)aName location:(NSString *)aLocation {
self = [super init];
if (self) {
self.name = aName;
self.location = aLocation;
}
return self;
}
-(id)initWithWithCoder:(NSCoder)decoder {
self = [super initWithCoder:decoder];
if (self) {
self.name = [decoder decodeObjectForKey:@"name"];
self.location = [decoder decodeObjectForKey:@"location";
}
return self;
}
-(void)encodeWithCoder:(NSCoder*)encoder {
[encoder encodeObject:self.name forKey:@"name"];
[encoder encodeObject:self.location forKey:@"location"];
[super encodeWithCoder:encoder];
}
@end
有了这个,将places
数组保存到磁盘就像下面这样简单:
[NSKeyedArchiver archiveRootObject:places toFile:path];
和解码一样简单:
places = [[KSKeyUnarchiver unarchiveObjectWithFile:path] retain];
要在数组中使用writeToFile对象,需要具有plist能力的类型(NSDate,NSDate,NSString,NSArray,NSDictionary)
对数组中的对象实现NSCoding并使用NSKeyedArchiver序列化/反序列化。 写:
[NSKeyedArchiver archiveRootObject:myArray toFile:self.places];
读:
[NSKeyedUnarchiver unarchiveObjectWithFile:path];
更多信息可以在这里找到:
坚持自定义对象
链接地址: http://www.djcxy.com/p/78143.html上一篇: Help with save file to Documents NSMutableArray with NSObjects