EXC_BAD_ACCESS KERN_INVALID_ADDRESS

时间:2017-06-23 07:28:43

标签: ios objective-c nsuserdefaults

崩溃了

  

EXC_BAD_ACCESS KERN_INVALID_ADDRESS

if(!app.settings.currentAuthToken.length)行中Settings是一个具有NSUserDefaults公共属性的类。 CurrentAuthToken是一个字符串属性。在app的逻辑中,可以在不同的线程中更改和读取此属性。可能会导致这次崩溃吗?

以下是Settings class:

的界面
@interface Settings : NSObject

@property (nonatomic, strong, readonly) NSUserDefaults* defaults;

@property (nonatomic, copy) NSString* currentUserId;

@property (nonatomic, copy) NSString* currentAuthToken;

@end

以下是Settings class:

中的方法代码
@synthesize defaults;
@dynamic currentUserId,currentAuthToken;

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

    }
    return self;
}

-(NSMethodSignature*)methodSignatureForSelector:(SEL)aSelector{
    if ([NSStringFromSelector(aSelector) hasPrefix:@"set"])
        return [super methodSignatureForSelector:@selector(setter:)];
    return [super methodSignatureForSelector:@selector(getter)];
}

-(id)getter{return nil;}
-(void)setter:(id)value{}

-(void)forwardInvocation:(NSInvocation *)anInvocation{
    NSString* key = NSStringFromSelector(anInvocation.selector);
    if ([key hasPrefix:@"set"]){
        key = [key substringFromIndex:3];
        key = [NSString stringWithFormat:@"%@%@",[[key substringToIndex:1] lowercaseString],[key substringWithRange:NSMakeRange(1, key.length-1)]];
        key = [key substringToIndex:key.length-1];

        __unsafe_unretained id argument;
        [anInvocation getArgument:&argument atIndex:2];

        [defaults setObject:argument forKey:key];
    }else{
            id returnValue = [defaults objectForKey:key];
            [anInvocation setReturnValue:&returnValue];
    }
}

0 个答案:

没有答案