0
votes

I get this error when I user dictionary set value for key:

* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionary0 0x6080000120d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key code_type.' * First throw call stack:
(
0 CoreFoundation 0x000000010adc7d4b exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010a05c21e objc_exception_throw + 48
2 CoreFoundation 0x000000010adc7c99 -[NSException raise] + 9
3 Foundation 0x0000000109b6a9df -[NSObject(NSKeyValueCoding) setValue:forKey:] + 291 4 KYGM 0x000000010826fe5f -[Lucky28BetVC networkForBet] + 1007
5 KYGM 0x000000010826f51c -[Lucky28BetVC judgeCondition] + 220
6 KYGM 0x000000010826f0da -[Lucky28BetVC betAction:] + 58
7 UIKit 0x000000010b1ed8bc -[UIApplication sendAction:to:from:forEvent:] + 83
8 UIKit 0x000000010b373c38 -[UIControl sendAction:to:forEvent:] + 67
9 UIKit 0x000000010b373f51 -[UIControl _sendActionsForEvents:withEvent:] + 444
10 UIKit 0x000000010b372e4d -[UIControl touchesEnded:withEvent:] + 668
11 UIKit 0x000000010b25b545 -[UIWindow _sendTouchesForEvent:] + 2747
12 UIKit 0x000000010b25cc33 -[UIWindow sendEvent:] + 4011
13 UIKit 0x000000010b2099ab -[UIApplication sendEvent:] + 371
14 UIKit 0x0000000119de3481 -[UIApplicationAccessibility sendEvent:] + 93
15 UIKit 0x000000010b9f672d __dispatchPreprocessedEventFromEventQueue + 3248
16 UIKit 0x000000010b9ef463 __handleEventQueue + 4879
17 CoreFoundation 0x000000010ad6c761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION
+ 17
18 CoreFoundation 0x000000010ad5198c __CFRunLoopDoSources0 + 556
19 CoreFoundation 0x000000010ad50e76 __CFRunLoopRun + 918
20 CoreFoundation 0x000000010ad50884 CFRunLoopRunSpecific + 420
21 GraphicsServices 0x000000010ed70a6f GSEventRunModal + 161
22 UIKit 0x000000010b1ebc68 UIApplicationMain + 159
23 KYGM 0x000000010825eecf main + 111
24 libdyld.dylib 0x000000010aa7f68d start + 1
) libc++abi.dylib: terminating with uncaught exception of type NSException

My error information is below:

enter image description here

My code is here:

    NSMutableArray *payJsonArr = [NSMutableArray arrayWithCapacity:0];

    for (int i = 0; i < self.dataSource.count; i ++) {

        id model = self.dataSource[i];

        NSDictionary *dict = [NSDictionary dictionary];

        NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
        Lucky28BetCell *cell = [self.tableView cellForRowAtIndexPath:indexpath];

        Lucky28BuyCodeModel *model_temp = (Lucky28BuyCodeModel *)model;

        if ([model_temp.type isEqualToString:@"num"]) {



            [dict setValue:((Lucky28BuyCodeModel *)model).type forKey:@"code_type"];  
            [dict setValue:((Lucky28BuyCodeModel *)model).num forKey:@"codes"];  
            [dict setValue:cell.money_tf.text forKey:@"money"];
            //[dict setValue:((Lucky28BuyCodeModel *)model).num forKey:@"BettingType"];  


        }else {

            [dict setValue:((Lucky28PalyModel *)model).type forKey:@"code_type"];  
            [dict setValue:((Lucky28PalyModel *)model).num forKey:@"codes"];  
            [dict setValue:cell.money_tf.text forKey:@"money"];  // 
        }

        [payJsonArr addObject:dict];
    }

My dataSource have two type model:

Lucky28BuyCodeModel:

#import <Foundation/Foundation.h>

@interface Lucky28BuyCodeModel : NSObject

@property (nonatomic, copy) NSString *num;  //
@property (nonatomic, copy) NSString *setting;  // 
@property (nonatomic, copy) NSString *type;  


- (NSComparisonResult)compareBuyCodeModel:(Lucky28BuyCodeModel *)model;

@end

And Lucky28PalyModel:

#import <Foundation/Foundation.h>

@interface Lucky28PalyModel : NSObject

@property (nonatomic, assign) BOOL isSelected; 

@property (nonatomic, copy) NSString *num;  
@property (nonatomic, copy) NSString *setting;  
@property (nonatomic, copy) NSString *type;  

- (NSComparisonResult)compareLucky28PalyModel:(Lucky28PalyModel *)model;

@end

How to solve this error in my code?

3
dict should be a NSMutableDictionary, not a NSDictionary, and prefer setObject:forKey: rather than setValue:forKey:. - Larme
Use NSMutableDictionary instead of NSDictionary if you want to set new value key. - nynohu
1. Change NSDictionary to NSMutableDictionary 2. ((Lucky28PalyModel *)model).type is int ? if so use it like @(((Lucky28PalyModel *)model).type) so it will be [dict setValue:@(((Lucky28PalyModel *)model).type) forKey:@"code_type"]; - iphonic

3 Answers

0
votes

there are two basic errors in your code

first one: you have to use NSMutableDictionary, so change the line below

NSDictionary *dict = [NSDictionary dictionary];

to

NSMutableDictionary *dict = [NSMutableDictionary dictionary];

then, the second one: you have to use the method setObject:forKey: (more at this link), so you should change the generic line below

[dict setValue:((Lucky28BuyCodeModel *)model).type forKey:@"code_type"];  

to

[dict setObject:((Lucky28BuyCodeModel *)model).type forKey:@"code_type"];  

finally, to "get" you have to use the method getObjectForKey: (more at this link)

0
votes

You are using NSDictionary to setValue:forKey:, you can not do that, you should use NSMutableDictionary.

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
0
votes

Use NSMutableDictionary:

NSMutableDictionary *dict = [NSMutableDictionary new];
...