0

I'm developing an iOS app. I have a class that extends NSObject that defines 1 property

//.h file
@property (nonatomic,retain) NSMutableDictionary *livedata; 


//.m file
if(self = [super init]){
    self.livedata = [[NSMutableDictionary alloc] init];
    [self.livedata setValue:@"myvalue" forUndefinedKey:@"myUndefinedKey"];
}

I'm getting this error. * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key myUndefinedKey.'

I've used NSMutableDictionary in the past for kvc, kvo. Yes I see the class is not kvc compliant.

zanemx
  • 59
  • 1
  • 5

2 Answers2

1

The problem is that you're calling a method that raises an exception when it is called. setValue:forUndefinedKey: is only called when setValue:forKey:, quoting this article, finds no property for a given key. Instead, just call

[self.livedata setValue:@"myvalue" forKey:@"myKey"];
Chris Loonam
  • 5,735
  • 6
  • 41
  • 63
0

You want setValue:forKey: not setValue:forUndefinedKey:

[self.livedata setValue:@"value" forKey:@"key"];

The system calls setValue:forUndefinedKey and the method is available to override.

From the Docs:
Invoked by setValue:forKey: when it finds no property for a given key.

"Subclasses can override this method to handle the request in some other way. The default implementation raises an NSUndefinedKeyException."

zaph
  • 111,848
  • 21
  • 189
  • 228