此前我一直在为我工作,但突然间它已经停止工作了。
我有Coupon
解析了一个对象JSONModel
,实际上该对象不是null,但是当我转换一些属性时,例如coupon.title
我得到了这个错误。
***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFDictionary标题]:无法识别的选择器发送到实例0x7f8510645ba0'
为什么会这样? 谢谢。
这是我的目标:
#import "JSONModel.h"
@protocol Coupon
@end
@interface Coupon : JSONModel;
@property (assign, nonatomic) NSString* title;
@property (strong, nonatomic) NSString* subtitle;
@property (strong, nonatomic) NSString* text;
@end
和json:
{
"subtitle":"ENDOR",
"title":"This IS THE OBJECT 1",
"text":"And this is the text of the coupon!!!"
}
答案 0 :(得分:2)
您没有在title属性中保留字符串;您应该查阅Apple内存管理文档:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html
答案 1 :(得分:1)
发现这个问题,试图弄清楚我自己的问题。
就我而言,问题是:
@property (assign, nonatomic) NSString* title;
vs
@property (strong, nonatomic) NSString* title;
马林托多罗夫是对的,但我花了一段时间才明白为什么失败了。库正在丢失引用,之后,当尝试获取值时,它无法解析数据。
答案 2 :(得分:0)
尝试使用copy
@property属性
@interface Coupon : JSONModel;
@property (copy, nonatomic) NSString* title;
@property (copy, nonatomic) NSString* subtitle;
@property (copy, nonatomic) NSString* text;
@end
此处有更多信息Objective-C declared @property attributes (nonatomic, copy, strong, weak)
如果你不确定,如果你甚至再收到JSON数据,你可以用这种代码进行调试:
// In case JSON parsing was successful:
NSLog(@"%@", json);
// In case JSON parsing failed:
NSLog(@"%@", [[NSString all] initWithData:json encoding:NSUTF8StringEncoding]);
答案 3 :(得分:-4)
嗯,问题出在我安装的Pod中,特别是this bug。