浮点数核心数据的问题 [英] Problems with Float on core-data

查看:139
本文介绍了浮点数核心数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码很大程度上受到我在网上发现的一些例子的启发,似乎工作得很好,核心数据实体称为Contact,而xcdatamodel中的属性名为address的属性为String。它可以毫无问题地保存我的数据。
现在我的问题是:我如何修改此代码?在xcdatamodel中将属性address的属性从String更改为Float后,为了使其工作。

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. 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.

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对象中,如下所示:

To store a float in a Core Data float attribute, wrap it in a NSNumber object like this:

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

这篇关于浮点数核心数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆