NSUserDefaults setObject ForKey:[CFNumber release]:发送给deallocated的消息

时间:2014-08-21 03:08:15

标签: ios objective-c automatic-ref-counting

我创建了自定义类来保存NSUserDefaults中的对象,如下所示:

@interface Config : NSObject
  + (Config*)sharedInstance;

  @property (nonatomic, retain) NSUserDefaults    *defaults;

  @property (nonatomic, retain) NSNumber          *isLogined;
  @properrty (nonatomic, retain) NSNumber          *isBindMobile;

  @property (nonatomic, retain) NSNumber          *currentLat;
  @property (nonatomic, retain) NSNumber          *currentLon;

@end

@implemention

 @dynamic isLogined;
 @dynamic userid;
 @dynamic isBindMobile;
 @dynamic currentLat;
 @dynamic currentLon;

+ (Config *)sharedInstance
{
static Config *config = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
    config = [[Config alloc] init];
});

return config;

}

- (id) init
{
if (self = [super init])
{
    self.defaults = [NSUserDefaults standardUserDefaults];

    NSDictionary *registrationDictionary = @{
                                             dIsLogined           : @NO,
                                             dPhoneno             : @"",
                                             dIsBindMobile        : @NO,
                                             dCurrentLat          : @0.0,
                                             dCurrentLon          : @0.0
                                             };
    [self.defaults registerDefaults:registrationDictionary];
}

return self;
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{

if ([NSStringFromSelector(aSelector) hasPrefix:@"set"])
{

    return [NSMethodSignature signatureWithObjCTypes:"v@:@"];
}

return [NSMethodSignature signatureWithObjCTypes:"@@:"];
}


- (void)forwardInvocation:(NSInvocation *)anInvocation
{

NSString *selector = NSStringFromSelector(anInvocation.selector);
if ([selector hasPrefix:@"set"])
{
    NSRange firstChar, rest;
    firstChar.location  = 3;
    firstChar.length    = 1;
    rest.location       = 4;
    rest.length         = selector.length - 5;

    selector = [NSString stringWithFormat:@"%@%@",
                [[selector substringWithRange:firstChar] lowercaseString],
                [selector substringWithRange:rest]];

    id value;
    [anInvocation getArgument:&value atIndex:2];

    if ([value isKindOfClass:[NSArray class]])
    {
        [self.defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:value] forKey:selector];
    }
    else
    {
        [self.defaults setObject:value forKey:selector];
    }
}
else
{
    id value = [self.defaults objectForKey:selector];

    if ([value isKindOfClass:[NSData class]])
    {
        value = [NSKeyedUnarchiver unarchiveObjectWithData:value];
    }

    [anInvocation setReturnValue:&value];

}
}

当我像这样setObject时:

[Config sharedInstance].currentLat = [NSNumber numberWithDouble: _location.coordinate.latitude];
[Config sharedInstance].currentLon = [NSNumber numberWithDouble: _location.coordinate.longitude];

应用程序将崩溃为 - [CFNumber release]:消息发送到解除分配的实例0x14d4b3f0。

如何解决这个问题?

0 个答案:

没有答案