2013-01-05 104 views
0

下面是一個方法的開始,它完成了相同的操作。但是我得到一個錯誤:無法調用NSManagedObject類上的指定初始化類'汽車'初始化對象時出錯

我在做什麼錯,謝謝。

- (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"]; 
    } 

回答

1

這是它的抱怨電話:

self = [super init]; 

後來你這樣做:

Car*coreDataCar =(Car*)[NSEntityDescription insertNewObjectForEntityForName:@"Car" inManagedObjectContext:[self managedObjectContext]]; 

你應該擺脫這兩個電話,並且像這樣的東西代替它們:

- (id) initWithJSONData:(NSDictionary *)responseData 
inManagedObjectContext:(NSManagedObjectContext *) moc { 

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Car" 
               inManagedObjectContext:moc]; 
    if (self = [super initWithEntity:entity insertIntoManagedObjectContext:moc]) { 

     ... 

    } 
    return self; 
} 

還要注意init方法的傳統返回類型爲id