-2

This has been working for me before, but suddenly it has stopped working.

I had an object Coupon parsed well by JSONModel, and indeed the object is not null, but when I cast some of the properties, for example coupon.title I get this error.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary title]: unrecognized selector sent to instance 0x7f8510645ba0'

Why is this happening? Thank you.

This is my object:

#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

and the json:

{
"subtitle":"ENDOR",
"title":"This IS THE OBJECT 1",
"text":"And this is the text of the coupon!!!"
}
4

4 回答 4

1

您没有在您的 title 属性中保留该字符串;您应该查阅 Apple 内存管理文档:https ://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html

于 2015-08-29T20:04:55.803 回答
1

发现了这个问题,试图找出我自己的问题。

就我而言,问题是:

@property (assign, nonatomic) NSString* title;

对比

@property (strong, nonatomic) NSString* title;

Marin Todorov 是对的,但我花了一段时间才明白为什么它失败了。库丢失了引用,后来在尝试获取值时,它无法解析数据。

于 2016-06-15T12:04:57.500 回答
0

尝试使用copy@property 属性保持数据可用:

@interface Coupon : JSONModel;
@property (copy, nonatomic) NSString* title;
@property (copy, nonatomic) NSString* subtitle;
@property (copy, nonatomic) NSString* text;
@end

更多信息在这里Objective-C 声明了@property 属性(非原子、复制、强、弱)

如果您不确定,如果您不再收到 JSON 数据,您可以使用以下代码对其进行调试:

// In case JSON parsing was successful:
NSLog(@"%@", json);
// In case JSON parsing failed:
NSLog(@"%@", [[NSString all] initWithData:json encoding:NSUTF8StringEncoding]);
于 2015-08-29T20:46:41.603 回答
-3

好吧,问题出在我安装的 Pod 中,尤其是这个 bug

于 2015-08-29T21:41:42.037 回答