그래서 객체를 응용 프로그램 샌드 박스 문서 디렉토리에 보관하려고합니다. Big Nerd Ranch의 iOS 프로그래밍에서 대부분의 코드를 복사 했으므로 잘못 될 수 있는지 모릅니다. 어쨌든 프로세스의 어느 부분이 작동하지 않는지 진단 할 수 있습니다. 그 중 일부는 오류를 발생시키지 않았지만 여전히 저장하지 못했습니다. 여기 내 객체 코드가있다.NSCoding을 사용한 iOS 보관 객체가 실패했습니다.
@interface Post : NSObject <NSCoding>
{
NSString *title;
NSString *date;
NSString *content;
NSString *type;
UIImage *image;
NSString *timestamp;
}
@property (nonatomic) NSString *title;
@property (nonatomic) NSString *date;
@property (nonatomic) NSString *content;
@property (nonatomic) NSString *type;
@property (nonatomic) UIImage *image;
@property (nonatomic) NSString *timestamp;
@end
@implementation Post
@synthesize title;
@synthesize date;
@synthesize content;
@synthesize type;
@synthesize image;
@synthesize timestamp;
#pragma mark - NSCoding Protocol
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:title forKey:@"title"];
[aCoder encodeObject:date forKey:@"date"];
[aCoder encodeObject:content forKey:@"content"];
[aCoder encodeObject:type forKey:@"type"];
[aCoder encodeObject:image forKey:@"image"];
[aCoder encodeObject:timestamp forKey:@"timestamp"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self){
[self setTitle:[aDecoder decodeObjectForKey:@"title"]];
[self setDate:[aDecoder decodeObjectForKey:@"date"]];
[self setContent:[aDecoder decodeObjectForKey:@"content"]];
[self setType:[aDecoder decodeObjectForKey:@"type"]];
[self setImage:[aDecoder decodeObjectForKey:@"image"]];
[self setTimestamp:[aDecoder decodeObjectForKey:@"timestamp"]];
}
return self;
}
는 내가 저장 applicationDidEnterBackground :와의 저장 방법을 호출 추적 방법
- (NSString *)itemArchivePath
{
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
//Get one and only document directory form that list
NSString *documentDirectory = [documentDirectories objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:@"posts.archive"];
}
- (BOOL)saveChanges
{
//return success of failure
NSString *path = [self itemArchivePath];
NSLog(@"save path = %@",path);
return [NSKeyedArchiver archiveRootObject:allPosts toFile:path];
}
와 저장소를 사용하여 보관 저장합니다.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
BOOL success = [[PostStore sharedStore] saveChanges];
if (success) {
NSLog(@"Saved all Posts");
} else {
NSLog(@"Could not save any Posts");
}
}
로
를 교체 NSDocumentationDirectory 대신 의 NSDocumentDirectory에 저장하는 당신은 "저장 실패"무엇을 의미합니까? 파일을 만들지 않습니까? 파일을 다시 읽을 수 없습니까? iPad는 지하 세계에 대한 관문을 열어 놓았습니다. – borrrden
그러나 주목할 점은 NSDocumentDirectory 대신 NSDocumentationDirectory를 사용했다는 것입니다. – borrrden