存档自定义对象时遇到问题

时间:2015-10-17 17:30:30

标签: objective-c parse-platform nscoder

所以我有这个定制的课程:

LocationSupplementaryObject.h:

@interface LocationSupplementaryObject : NSObject <NSCoding>

@property (nonatomic) NSString* storedPlaceName;

-(id)initWithLocationName:(NSString*)locationName;
-(void)encodeWithCoder:(NSCoder*)encoder;
-(id)initWithCoder:(NSCoder*)decoder;

- (void)saveCustomObject:(LocationSupplementaryObject *)object key:(NSString *)key;
- (LocationSupplementaryObject *)loadCustomObjectWithKey:(NSString *)key;
@end

LocationSupplementaryObject.m:

@implementation LocationSupplementaryObject

- (id)initWithLocationName:(NSString *)locationName
{
    self = [super init];
    if (self) {
        self.storedPlaceName = locationName;
        NSLog(@"Initialization success! Name is %@",self.storedPlaceName);
    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)encoder {
    // Encode the properties of the object
    [encoder encodeObject:self.storedPlaceName forKey:@"stored_placeName"];
}

-(id)initWithCoder:(NSCoder *)decoder {
    self = [super init];
    if (self) {
        // Decode properties for the object
        self.storedPlaceName = [decoder decodeObjectForKey:@"stored_placeName"];
    }
    return self;
}

- (void)saveCustomObject:(LocationSupplementaryObject *)object key:(NSString *)key {
    NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:encodedObject forKey:key];
    [defaults synchronize];

}

- (LocationSupplementaryObject *)loadCustomObjectWithKey:(NSString *)key {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *encodedObject = [defaults objectForKey:key];
    LocationSupplementaryObject *object = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
    return object;
}

@end

所以稍后在我的程序中我使用 LocationSupplementaryObject 类:

LocationSupplementaryObject *suppObject = [[LocationSupplementaryObject alloc]initWithLocationName:[object objectForKey:(NSString*)kCCName]];

//  Writing the object to defaults

    [suppObject saveCustomObject:suppObject key:@"StoredLocation"];
    NSLog(@"%@ Successfully packed",suppObject.storedPlaceName);

    LocationSupplementaryObject *newObject = [[LocationSupplementaryObject alloc]init];
    [newObject loadCustomObjectWithKey:@"StoredLocation"];
    NSLog(@"Re initializing from defaults %@",newObject.storedPlaceName);

要初始化名称,我正在从parse.com加载对象,我们可以看到通过NSLog消息正确设置了suppObject.storedPlaceName。 但是,从NSUserDefaults检索和取消归档的名称原来是(null),我知道NSUserDefaults可能很慢,即使我们调用了synchronize所以我正在检查程序另一端的对象:

LocationSupplementaryObject *suppObject = [[LocationSupplementaryObject alloc]init];
[suppObject loadCustomObjectWithKey:@"StoredLocation"];
if (suppObject) {
NSLog(@"We got our name %@",suppObject.storedPlaceName);
}

尽管类是使用合法的NSString初始化的,但它仍然显示存储在NSUserDefaults中的名称(null)。 我应该承认,我对NSCoder没有很多经验,所以任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

您的加载和保存方法应该是类方法。将它们创建为实例方法意味着您将获得使用而不处理返回值。所以你期望成为一件事的对象实际上并非如此。你的编码器代码实际上很好。