Below is the beginning of a method that does the same thing all the way down. However I get an error: Failed to call designated initializer on NSManagedObject class 'Car'
What am I doing wrong, thank you.
- (Car*) initWithJSONData:(NSDictionary *)responseData {
BOOL isActive;
self = [super init];
Car*coreDataCar =(Car*)[NSEntityDescription insertNewObjectForEntityForName:@"Car" inManagedObjectContext:[self managedObjectContext]];
//Check if car is active or deactive.
if (![responseData valueForKeyIsNull:@"Car Status"]) {
int carStatus = [[responseData valueForKey:@"Car Status"] intValue];
if (carStatus == 1 ) {//active
isActive=YES;
}else if( carStatus == 2 ){
isActive = NO;
}
}
if (![responseData valueForKeyIsNull:@"Name"]) {
[coreDataCar setValue:[responseData valueForKey:@"Name"] forKey:@"name"];
}
NSLog(@"user count = %@",[responseData valueForKey:@"UserCount"]);//Sometimes it crashes here, others it doesnt
if (![responseData valueForKeyIsNull:@"UserCount"]) {
//[responseData valueForKey:@"UserCount"];
[coreDataCar setValue:[responseData valueForKey:@"UserCount"] forKey:@"userCount"];
}
This is the call it's complaining about:
self = [super init];
Later you do this:
Car*coreDataCar =(Car*)[NSEntityDescription insertNewObjectForEntityForName:@"Car" inManagedObjectContext:[self managedObjectContext]];
You should get rid of both of those calls and replace them with something like this:
- (id) initWithJSONData:(NSDictionary *)responseData
inManagedObjectContext:(NSManagedObjectContext *) moc {
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Car"
inManagedObjectContext:moc];
if (self = [super initWithEntity:entity insertIntoManagedObjectContext:moc]) {
...
}
return self;
}
Note also that init methods conventionally have a return type of id
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.