【发布时间】:2023-03-27 13:40:01
【问题描述】:
尝试在对象中存储双精度时出现错误
error Sending 'double' to parameter of incompatible type 'id'
我的代码是:
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Contacts" inManagedObjectContext:context];
[newDevice setValue:longitudes forKey:@"longitude"];
[newDevice setValue:latitudes forKey:@"latitude"];
setValue:longitudes
longitudes 是 double 类型。
【问题讨论】:
-
longitudes和latitudes的类型是什么? -
[newDevice setValue:@(longitudes) forKey:@"longitude"];和[newDevice setValue:@(latitudes) forKey:@"latitude"];,也许? -
好的,您不能将原始类型的值传递给
-setValue:forKey:方法。值应该是 id 类型(任何 Objective-c 类)。 Ho to cast double to NSNumber 已在其他答案中显示。 -
附带说明:您可以继承 NSManagedObject(使用 XCode 的生成器)。然后,您将拥有显式类型的属性,这使开发更容易。
标签: ios objective-c xcode core-data xcode6