Question

As descriped in the documentation I tried to bind some properties to the NSUserDefaults. Now I'm getting a EXC_BAD_ACCESS (code=2, address=0x10)) exception. What is wrong with the code?

@interface Settings:NSObject
  @property (copy, nonatomic) NSString *username;
  @property (copy, nonatomic) NSString *password;
@end


static NSString *const USERNAME_KEY = @"username";
static NSString *const PASSWORD_KEY = @"password";
@implementation Setting
{
   NSUserDefaults *_defaults;
}

-(instancetype) init {
  self = [super init];
  if (self) {
   _defaults = [NSUserDefaults standardUserDefaults];
   RACChannelTo(self, username, @"") = [_defaults rac_channelTerminalForKey:USERNAME_KEY];
   RACChannelTo(self, password, @"") = [_defaults rac_channelTerminalForKey:PASSWORD_KEY];
   [[NSNotificationCenter defaultCenter]
            addObserver:self
               selector:@selector(defaultsDidChange:)
                   name:NSUserDefaultsDidChangeNotification
                 object:nil];
 }
return self;
}

The Exception is thrown in the class RACKVOTrampoline in the method initWithTarget on the line [self.target addObserver:self forKeyPath:self.keyPath options:options context:&RACKVOWrapperContext];.

Was it helpful?

Solution

Ok. While creating the test project I found the problem. As stupid as it was, the class which created the Settings object wasn't registered for the test target. Don't ask me why it was compiling and running until it crashed...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top