开发者

CoreData inserting new object for entity is giving me error when using setValue: forKey: with an int

开发者 https://www.devze.com 2023-02-03 10:57 出处:网络
Here is the code: ComicDB * newComic = (ComicDB *)[NSEntityDescription insertNewObjectForEntityForName:@\"ComicDB\" inManagedObjectContext:context];

Here is the code:

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"];

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

when i change the line of code to:

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

i get these warnings and an error: warning: invalid receiver t开发者_开发百科ype 'int' warning: passing argument 1 of 'setValue:forKey:' makes pointer from integer without a cast

What am i doing wrong?


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"];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号