Pergunta

So I want to archive my object into by app sandbox documents directory. I copied most of the code from Big Nerd Ranch's iOS programming, so I don't see what could possibly go wrong. Is there anyway I could diagnose which part of the process is not working, since non of them is raising any error, but still failed to save. Here is my object code.

@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;
}

I save archive it using a store with the follow methods

- (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];
}

I called the save method in applicationDidEnterBackground to save.

- (void)applicationDidEnterBackground:(UIApplication *)application
{    
    BOOL success = [[PostStore sharedStore] saveChanges];
    if (success) {
        NSLog(@"Saved all Posts");
    } else {
        NSLog(@"Could not save any Posts");
    }
}
Foi útil?

Solução

You are saving to NSDocumentationDirectory instead of NSDocumentDirectory

Replace

   NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);  

With

  NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top