当我使用字典`set value for key`时出现此错误
Posted
技术标签:
【中文标题】当我使用字典`set value for key`时出现此错误【英文标题】:I get this error when I user dictionary `set value for key` 【发布时间】:2017-10-10 03:41:22 【问题描述】:当我使用字典 set value for key
时出现此错误:
* 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[<__nsdictionary0> setValue:forUndefinedKey:]:此类与键 code_type 的键值编码不兼容。” * 首先抛出调用栈: ( 0 CoreFoundation 0x000000010adc7d4b 异常预处理 + 171 1 libobjc.A.dylib 0x000000010a05c21e objc_exception_throw + 48 2 CoreFoundation 0x000000010adc7c99 -[NSException raise] + 9 3 基础 0x0000000109b6a9df -[NSObject(NSKeyValueCoding) setValue:forKey:] + 291 4 KYGM 0x000000010826fe5f -[Lucky28BetVC networkForBet] + 1007 5 KYGM 0x000000010826f51c -[Lucky28BetVC 判断条件] + 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 核心基础 0x000000010ad6c761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 18 核心基础 0x000000010ad5198c __CFRunLoopDoSources0 + 556 19 核心基础 0x000000010ad50e76 __CFRunLoopRun + 918 20 核心基础 0x000000010ad50884 CFRunLoopRunSpecific + 420 21 图形服务 0x000000010ed70a6f GSEventRunModal + 161 22 UIKit 0x000000010b1ebc68 UIApplicationMain + 159 23 KYGM 0x000000010825eecf 主 + 111 24 libdyld.dylib 0x000000010aa7f68d 开始 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止
我的错误信息如下:
我的代码在这里:
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];
我的数据源有两种类型的模型:
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
还有 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
如何解决我的代码中的这个错误?
【问题讨论】:
dict
应该是 NSMutableDictionary
,而不是 NSDictionary
,并且更喜欢 setObject:forKey:
而不是 setValue:forKey:
。
如果要设置新的值键,请使用 NSMutableDictionary
而不是 NSDictionary
。
1.将 NSDictionary
更改为 NSMutableDictionary
2. ((Lucky28PalyModel *)model).type
是 int 吗?如果是这样使用它就像@(((Lucky28PalyModel *)model).type)
这样它将是[dict setValue:@(((Lucky28PalyModel *)model).type) forKey:@"code_type"];
【参考方案1】:
您的代码中有两个基本错误
第一个:你必须使用NSMutableDictionary
,所以改变下面的行
NSDictionary *dict = [NSDictionary dictionary];
到
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
然后,第二个:你必须使用方法setObject:forKey:
(更多在this link),所以你应该改变下面的通用行
[dict setValue:((Lucky28BuyCodeModel *)model).type forKey:@"code_type"];
到
[dict setObject:((Lucky28BuyCodeModel *)model).type forKey:@"code_type"];
最后,要“获取”你必须使用方法getObjectForKey:
(更多信息请参见this link)
【讨论】:
【参考方案2】:你正在使用 NSDictionary 到setValue:forKey:
,你不能这样做,你应该使用 NSMutableDictionary。
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
【讨论】:
【参考方案3】:使用 NSMutableDictionary:
NSMutableDictionary *dict = [NSMutableDictionary new];
...
【讨论】:
以上是关于当我使用字典`set value for key`时出现此错误的主要内容,如果未能解决你的问题,请参考以下文章