-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!!!"
}
Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92
  • Can you post the JSON data you're working with? – scunliffe Aug 29 '15 at 19:47
  • 1
    without any details nobody can answer your question – Marin Todorov Aug 29 '15 at 20:00
  • Added! sorry about that – Rafael Ruiz Muñoz Aug 29 '15 at 20:04
  • also, it would be even easier if you show the implementation file, but I don't know if you are using one – Larry Pickles Aug 29 '15 at 20:18
  • It looks like you are trying to pass a `title` message to dictionary. Not to a `Coupon`. Are you calling `someJsonstring.title` instead of `someCouponInstance.title`? What do you mean when you say cast? Are you attempting to do something like `[(Coupon *) someJsonString title]`? Because this won't work. – DeepFriedTwinkie Aug 29 '15 at 20:21
  • 2
    Please someone else explain better than me that the value stored in the title property is being released prematurely in simpler words than me - it looks like I'm not getting trough ... – Marin Todorov Aug 29 '15 at 20:23
  • no @DeepFriedTwinkie, indeed it was working some hours ago... I don't know what happened. – Rafael Ruiz Muñoz Aug 29 '15 at 20:24
  • I'm extremely curious to see what kind of answer is going to get accepted... – Marin Todorov Aug 29 '15 at 20:26
  • 1
    Well then, like @MarinTodorov said: an hour ago `title` was pointing to a string. (Probably because the json string was still retained and in memory.) But, the json string probably got released. So now title is pointing to garbage. Because you are not retaining the string in your title property. – DeepFriedTwinkie Aug 29 '15 at 20:34

4 Answers4

1

You are not retaining the string in your title property; you should consult the Apple memory management docs: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html

Marin Todorov
  • 6,377
  • 9
  • 45
  • 73
1

Found this question, trying to figuring out a problem of my own.

In my case the problemas was:

@property (assign, nonatomic) NSString* title;

vs

@property (strong, nonatomic) NSString* title;

Marin Todorov was right, but it took me a while to understand why it was failing. The library was losing the reference and later, when trying to fetch the value, it couldn't parse the data.

dequin
  • 906
  • 12
  • 19
0

Try to keep the data available afterwards with copy @property attribute:

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

More info here Objective-C declared @property attributes (nonatomic, copy, strong, weak)

In case you're not sure, if you even receive JSON data any more, you could debug it with this kind of code:

// In case JSON parsing was successful:
NSLog(@"%@", json);
// In case JSON parsing failed:
NSLog(@"%@", [[NSString all] initWithData:json encoding:NSUTF8StringEncoding]);
Community
  • 1
  • 1
JOM
  • 8,139
  • 6
  • 78
  • 111
-3

Well, the problem was residing in a Pod that I've installed, in particular this bug.

halfer
  • 19,824
  • 17
  • 99
  • 186
Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92
  • Hi Rafael. I've not voted on your question, but we do ask that commentary about voting (or its fairness) is kept to comments. If you think you've been particularly badly treated then you can always post to Meta. In the meantime I suggest you delete this answer and repost the answer information. If you can expand on the single sentence, you may even get upvotes! – halfer Aug 31 '15 at 11:50