简体   繁体   English

浮点数核心数据的问题

[英]Problems with Float on core-data

The following code largely inspired by some example I found on the net seems to work fine, with the core data entity called "Contact" and the property called "address" having an attribute String, in the xcdatamodel. 下面的代码很大程度上受到我在网上发现的一些例子的启发,似乎工作得很好,核心数据实体称为“Contact”,而xcdatamodel中的属性名为“address”,属性为String。 It saves my data with no problem. 它可以毫无问题地保存我的数据。 Now my question is : how do I need to modify this code ? 现在我的问题是:我如何修改此代码? In order to make it work after I change the attribute of the property "address" from String to Float in the xcdatamodel. 在xcdatamodel中将属性“address”的属性从String更改为Float后,为了使其工作。

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浮点属性中,请将其包装在NSNumber对象中,如下所示:

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

This is a guess, but I think you will need to wrap that float in a NSNumber. 这是一个猜测,但我认为你需要将该浮动包装在NSNumber中。 numberWithFloat: numberWithFloat:

Creates and returns an NSNumber object containing a given value, treating it as a float.

+ (NSNumber *)numberWithFloat:(float)value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM