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

Having trouble archiving custom object

所以我要定制这个 class:

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 class:

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 检索和取消存档的名称结果是(空),我知道 NSUserDefaults 可能很慢,即使我们调用了同步所以我正在程序的另一端检查对象:

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

即使 class 是用合法的 NSString 初始化的,它仍然显示我 (null) 存储在 NSUserDefaults 中的名称。 我应该承认,我没有太多使用 NSCoder 的经验,所以任何帮助将不胜感激。

您的加载和保存方法应该是 class 方法。将它们创建为实例方法意味着您会感到困惑而不是处理 return 值。所以你期望成为一件事的对象实际上不是。您的编码器代码实际上很好。