0

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];.

melbic
  • 11,988
  • 5
  • 33
  • 37
  • What is `RACChannelTo`? Where is the error happening? – rmaddy Apr 17 '14 at 15:47
  • I edited the question more information to RACChannelTo [here](https://github.com/ReactiveCocoa/ReactiveCocoa/blob/master/ReactiveCocoaFramework/ReactiveCocoa/RACKVOChannel.h) – melbic Apr 17 '14 at 15:51
  • You're going to need to post more complete code if you want help diagnosing the problem. For example, what is `_defaults`? Where is the rest of your `init` method? – Ian Henry Apr 17 '14 at 16:27
  • I added the rest of the init method for you, but it really isn't anything suprising with it. – melbic Apr 18 '14 at 15:41

1 Answers1

0

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...

melbic
  • 11,988
  • 5
  • 33
  • 37