整数到NSInteger并保存到核心数据 [英] Integer to NSInteger And Save To Core Data

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

问题描述

我有一个名为marbles的整数,并且试图使用以下代码将其保存到数组中:

I have a integer named marbles, and am trying to save it into an array using the following code:

[records setValue:marbles forKey:@"marbles"];

使用此代码,我会收到以下警告:

With this code, I get the warning:

警告:语义问题:不兼容的整数到指针的转换发送'int'到类型'id'的参数

warning: Semantic Issue: Incompatible integer to pointer conversion sending 'int' to parameter of type 'id'

那么,如何设置NSInteger的值。

So, How do I set the value for an NSInteger.

下一个问题,如何将数组重新上传到核心数据?

Next Question, How do re-upload the array into core data? I fetch the array, make changes, and how do I apply those changes back to Core Data?

感谢

推荐答案

使用您的整数初始化NSNumber(这是CoreData所期望的):

Initialize an NSNumber (which is what CoreData is expecting) with your integer:

NSNumber *marbleNumber = [NSNumber numberWithInt:marbles];
[records setObject:marbleNumber forKey@"marbles"];

或:

[records setMarbles:[NSNumber numberWithInt:marbles]];

要保存更改,请保存上下文:

To persist your changes, you save your context:

NSError *error;
[myManagedObjectContext save:&error];

//handle your error

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

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