崩溃了
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];
}
}