本文介绍了使用setValue:forKey:与int时,CoreData为实体插入新对象给我错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是代码:

ComicDB * newComic = (ComicDB *)[NSEntityDescription insertNewObjectForEntityForName:@"ComicDB" inManagedObjectContext:context];
            [newComic setValue:comic.iComicId forKey:@"ComicID"];
            [newComic setValue:comic.sImage forKey:@"Image"];
            [newComic setValue:comic.sName forKey:@"Name"];
            [newComic setValue:comic.sText forKey:@"Text"];
            [newComic setValue:comic.sComicURL forKey:@"URL"];

这是我在 setValue:comic.iComicId forKey @上收到的警告 ComicID 。仅供参考-漫画是我自己的课程,其中包含漫画的详细信息。
警告:传递'setValue:forKey:'的参数1使指针从整数转换而无需强制转换

This was the warning i got on the setValue:comic.iComicId forKey@"ComicID". FYI - comic is my own class that holds comic details.warning: passing argument 1 of 'setValue:forKey:' makes pointer from integer without a cast

当我将代码行更改为:

[newComic setValue:[comic.iComicId intValue] forKey:@"ComicID"];

我收到以下警告和错误:
警告:无效的接收器类型'int'
警告:传递'setValue:forKey:'的参数1使指针从整数转换而无需强制转换

i get these warnings and an error:warning: invalid receiver type 'int'warning: passing argument 1 of 'setValue:forKey:' makes pointer from integer without a cast

我在做什么错了?

推荐答案

如果 comic.iComicId 不是 NSNumber ,则需要将其包装在 NSNumber 对象中,然后将其传递:

If comic.iComicId is not an NSNumber, you need to wrap it in an NSNumber object then pass it:

[newComic setValue:[NSNumber numberWithInt:[comic.iComicId intValue]] forKey:@"ComicID"];

这篇关于使用setValue:forKey:与int时,CoreData为实体插入新对象给我错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 17:31