核心数据上的浮点问题

下面的代码很大程度上受到我在网上find的一些例子的启发,似乎在xcdatamodel中工作正常,核心数据实体名为“Contact”,而名为“address”的属性具有属性String。 它保存我的数据没有问题。 现在我的问题是:我该如何修改这个代码? 为了使它在xcdatamodel中将属性“地址”从string更改为浮点后的工作。

CoreDataTestOneAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = [appDelegate managedObjectContext]; NSManagedObject *newContact; newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Contacts" inManagedObjectContext:context]; [newContact setValue:address_InputField.text forKey:@"address"]; NSError *error; [context save:&error]; 

要将一个浮点数存储在Core Data float属性中,将其包装在NSNumber对象中,如下所示:

 [newContact setValue:[NSNumber numberWithFloat:floatValue] forKey:@"address"]; 

这是一个猜测,但我认为你将需要包装在NSNumber浮动。 numberWithFloat:

 Creates and returns an NSNumber object containing a given value, treating it as a float. + (NSNumber *)numberWithFloat:(float)value