我是一名新手 iOS 开发人员。
我编写了一个小应用程序,它保存了一个 NSMutableArray 数组,其中包含从 NSObject 派生的对象。
应用程序进行保存,但文件未在文档目录中创建,应用程序无法读取。
这个问题在模拟器和我的 iPhone 3gs 4.2.1 上都有
我在 appDelegate 类中的 NSMutableArray 定义:
@property (nonatomic,retain, readwrite) NSMutableArray *places;
我的 NSObject 类:
#import <Foundation/Foundation.h>
@interface Place : NSObject {
NSString *name;
NSString *location;
}
-(id) initNSString *)name: (NSString *)location;
@property (retain,nonatomic,readwrite) NSString *name;
@property (retain,nonatomic,readwrite) NSString *location;
@end
我的 StorageService 库类:
#import "StorageService.h"
@implementation StorageService
-(id) init {
self = [super init];
if (self != nil) {
}
return self;
}
-(void) saveArrayToFileNSString*) filename : (NSMutableArray *)arrayToSave{
// get full path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fullPath = [paths objectAtIndex:0];
fullPath = [fullPath stringByAppendingPathComponent:filename];
NSLog(@"Save in %@",fullPath);
[arrayToSave writeToFile:fullPath atomically:YES];
}
-(NSMutableArray*) readArrayFromFileNSString *)filename {
// get full path
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];
}
@end
和appDelegate 中的My函数:
-(void) saveApplicationData {
[self.storageService saveArrayToFile : PLACES_FILE : self.places];
}
-(void) loadApplicationData {
self.places = [self.storageService readArrayFromFileLACES_FILE];
}
这是我的类,它对文件名保持不变:
#import <Foundation/Foundation.h>
extern NSString * const PLACES_FILE = @"laces.dat";
@interface ApplicationConstants : NSObject {
}
@end
那么有什么问题呢?
谢谢你们。
Best Answer-推荐答案 strong>
您想要的是让 Place 符合 NSCoding 协议(protocol),以允许对文件进行序列化(如果需要,还可以在内存中数据)
将 Place 扩展为 (我还更改了 init 方法的名称,因为您的名字违反了 iOS 的所有命名惯例):
@interface Place : NSObject <NSCoding> {
NSString *name;
NSString *location;
}
-(id)initWithNameNSString *)name locationNSString *)location;
@property (retain,nonatomic,readwrite) NSString *name;
@property (retain,nonatomic,readwrite) NSString *location;
@end
您的实现非常简单,但您还需要实现 NSCoding 协议(protocol)定义的两个方法:
@implementation Place
@synthesize name, location;
-(id)initWithNameNSString *)aName locationNSString *)aLocation {
self = [super init];
if (self) {
self.name = aName;
self.location = aLocation;
}
return self;
}
-(id)initWithWithCoderNSCoder)decoder {
self = [super initWithCoder:decoder];
if (self) {
self.name = [decoder decodeObjectForKey"name"];
self.location = [decoder decodeObjectForKey"location";
}
return self;
}
-(void)encodeWithCoderNSCoder*)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];
关于objective-c - 使用 NSObjects 将文件保存到文档 NSMutableArray 的问题,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/7038428/
|