Reputation: 9131
I am trying to parse some JSON for my app.
When trying to set the "name" value I get the error unrecognized selector sent to instance'
.
This is the line of code the error happens on: self.name = [json_object objectForKey:@"name"];
(json_object is an NSDictionary and "self" is an object in CoreData)
My JSON looks like this:
json_object: {
draftstatus = "isdraftfull.php";
getcard = "getcard.php";
getpack = "getpack.php";
imageextension = ".jpg";
images = "images/small/";
joindraft = "joindraft.php";
joindraftmenu = "getjoindraftmenu.php";
login = "loginasuser.php";
name = "the server";
passpack = "passpack.php";
playerdraftstatus = "getplayerdraftstatus.php";
signup = "signup.php";
userexists = "checkifuserexists.php";
usesetidforimagepath = false;
}
Server.h looks like this:
#import <CoreData/CoreData.h>
#import "JSON.h"
@interface Server : NSManagedObject
{
NSMutableData *responseData;
}
@property (nonatomic, retain) NSString * playerdraftstatus;
@property (nonatomic, retain) NSString * signup;
@property (nonatomic, retain) NSNumber * usesetidforimagepath;
@property (nonatomic, retain) NSString * login;
@property (nonatomic, retain) NSString * imageextension;
@property (nonatomic, retain) NSString * userexists;
@property (nonatomic, retain) NSString * getcard;
@property (nonatomic, retain) NSString * passpack;
@property (nonatomic, retain) NSString * draftstatus;
@property (nonatomic, retain) NSString * joindraftmenu;
@property (nonatomic, retain) NSString * getpack;
@property (nonatomic, retain) NSString * joindraft;
@property (nonatomic, retain) NSString * images;
@property (nonatomic, retain) NSNumber * isdefaultserver;
@property (nonatomic, retain) NSString * root;
@property (nonatomic, retain) NSString * name;
- (id) initWithWebAddress:(NSString *) address;
- (id) initWithWebAddressAsyn:(NSString *) address;
- (void) fillFromJSON:(NSDictionary *) json_object;
- (void) saveServer;
@end
I believe it was working a few days ago, but I don't remember. When I started working on it today I added the "root" object. It compiles with no errors but won't parse that json.
Help?
EDIT
I changed the JSON to come back with name = "the server"
and when I log that like this NSLog(@"json_object name: %@", [json_object objectForKey:@"name"]);
the log says json_object name: the server
but I still get the same error when I try to set that to self.name
which is of type String.
I did notice that sometimes when examining the Log version of the JSON that the value inside name isn't always surrounded in quotes and as you see from the log of [json_object objectForKey:@"name"]
it is showing without quotes around it. Is that the problem and why is that happening? If I look at the json straight on the web page that outputs it, there is ALWAYS quotes around the value.
EDIT 2
The full error is as follows:
-[Server setName:]: unrecognized selector sent to instance 0x61036e0
2010-11-19 15:46:10.113 TCGDraft[57953:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Server setName:]: unrecognized selector sent to instance 0x61036e0'
*** Call stack at first throw:
EDIT 3
I did an NSLog on "self" and this is what comes back self: <Server: 0x6143190> (entity (null); id: (null) ; data: {})
I have no clue if that is what is normal though. This is a CoreData object and I have a feeling I might have messed it up somehow.
EDIT 4
I went back and looked at the code to create a Server Object from CoreData like this:
Server *server = [NSEntityDescription
insertNewObjectForEntityForName:@"Server"
inManagedObjectContext:context];
When I ran that I now get an error that just says EXC_BAD_ACCESS
so it does look like somehow that object is messed up...but I don't know how.
EDIT 5
Right, so I completely deleted the Server.h and Server.m files with my custom code, I recreated them from the CoreData object and tried that code I posted in "Edit 4" and I STILL get the same error about EXC_BAD_ACCESS....so now I have apparently gone BACKWARD rather than Forward.
Upvotes: 1
Views: 1961
Reputation: 18670
Firstly, I suggest you separate the NSManagedObject from your JSON parser methods completely. All the async loading and processing of your JSON data should be dealt with in a separate object that you can then assign to your managed object properties.
#import <CoreData/CoreData.h>
@interface Server : NSManagedObject
{
}
@property (nonatomic, retain) NSString * playerdraftstatus;
@property (nonatomic, retain) NSString * signup;
...
@property (nonatomic, retain) NSString * name;
@end
Secondly, DO NOT change the @dynamic values back to @synthesize otherwise CoreData will not be able to manipulate and save these objects for you (that's most likely why you are getting the EXEC_BAD_ACCESS exception when you try and save the object!).
@dynamic *playerdraftstatus;
@dynamic *signup;
...
@dynamic *name;
Now try assigning the attribute values from the controller where you are creating your Server object i.e.
Server *server = [NSEntityDescription
insertNewObjectForEntityForName:@"Server"
inManagedObjectContext:context];
server.name = [json_object valueForKey:@"name"];
And finally, make sure you don't ever release the server object as CoreData will look after this for you automatically.
PS: if you changed your data model you may have to reset/delete the app from the iPhone/Simulator otherwise you will get an error message saying the store type is incompatible with the existing one on the phone etc.
Let me know how you go and if you get any further error messages.
Cheers,
Rog
Upvotes: 3
Reputation: 9601
The problem is that you don't have a setName method in your Server class. It has nothing to do with the json data. Make sure you synthesize your properties, or otherwise define the requisite methods.
Upvotes: 0