Reputation: 235
I have a core data model and a database built in JSON format. I send web request to the database and then I create a new object in the core data model with the values extracted. Strings work fine, but I am having a problem with extracting the float number. I cannot make it work properly.
float score;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
NSMutableDictionary *prodData = [NSJSONSerialization
JSONObjectWithData:returnData
options:kNilOptions
error:&error];
TempClass* tempClass= [NSEntityDescription
insertNewObjectForEntityForName:@"TempCode"
inManagedObjectContext:context];
tempClass.score = [[prodData valueForKey:@"score"] floatValue];
Error:
* thread #1: tid = 0x92b05, 0x01b270b0 libobjc.A.dylib`objc_msgSend + 12, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x40600000)
Already tried:
tempClass.score = prodData[@"score"];
and
float scoret = [[prodData valueForKey:@"score"] floatValue];
tempClass.score = &(scoret) ;
Upvotes: 0
Views: 93
Reputation: 235
Fixed the EXC_BAD_ACCESS, by converting the float to NSNumber. Then logging NSNumber on float format.
Upvotes: 0
Reputation: 5162
Assigning to (float*) from incompatible type
float
This implies that you have declared 'score' as an instance of 'float'. i.e as float *score;
Remove the '*' please. float is a primitive data type.
Its also a good practise to cast the serialised JSON data (just to be on the safer side).
id _castIf(Class requiredClass, id object) {
if (object && ![object isKindOfClass:requiredClass])
object = nil;
return object;
}
And can be used as:
_castIf(NSNumber,prodData[@"score"]);
Updated:
EXC_BAD_ACCESS
errors occur mostly in scenarios like:
Some ways to debug this is by going backwards on your stack of backtrace, using NSZombies, perhaps a good browse through web might also help; there are lots of resources already.
Upvotes: 1