无法使用类型(Int64,String)的参数列表调用setValue

编程入门 行业动态 更新时间:2024-10-28 13:20:59
本文介绍了无法使用类型(Int64,String)的参数列表调用setValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经检查了这里的答案SO和谷歌和我stumped。

func saveAvailableWord(word:GameWord){ let gw = NSEntityDescription.insertNewObjectForEntityForName(GameWord,inManagedObjectContext:persistence .managedObjectContext!)as! NSManagedObject let rId = word.rowID gw.setValue(rId,forKey:rowID)// ***错误行}

上面的代码用于核心数据,只是尝试创建一个对象。但我得到

无法使用类型为'(Int64,forKey:String)'的参数列表调用'setValue'

。rowID定义为 @NSManaged var rowID:Int64

如果我用一个简单的数字替换rId变量,错误消失,我缺少什么?

感谢您的时间和帮助。

解决方案

pre> func setValue(value:AnyObject?,forKey key:String)

期望一个(可选)对象作为第一个参数不像 Int ,固定的大小整数类型 Int64 , Int32 ,...不是自动桥接到 NSNumber 对象,因此必须明确调用

gw.setValue ),forKey:rowID)

但如果 rowId 在托管​​对象子类中定义为 Int64 ,那么您可以简单地使用

gw.rowID = rID

对于键值编码方法。

I've checked the answers here on SO and google and am stumped.

func saveAvailableWord(word: GameWord) { let gw = NSEntityDescription.insertNewObjectForEntityForName("GameWord", inManagedObjectContext: persistence.managedObjectContext!) as! NSManagedObject let rId = word.rowID gw.setValue(rId, forKey: "rowID") //*** Error line }

The code above is for core data and simply tries to create an object. But I get

"Cannot invoke 'setValue' with an argument list of type '(Int64, forKey: String)'

on the marked line. rowID is defined as @NSManaged var rowID: Int64 because it is coming from an SQLite ROWID column.

If I replace the rId variable with a simple number the error goes away. What am I missing? What am I doing wrong?

Thanks for your time and help.

解决方案

func setValue(value: AnyObject?, forKey key: String)

expects an (optional) object as the first argument. Unlike Int, the fixed size integer types Int64, Int32, ... are not automatically bridged to NSNumber objects, so you have to call explicitly

gw.setValue(NSNumber(longLong: rID), forKey: "rowID")

But if rowId is defined as Int64 in the managed object subclass, then you can simply assign the property value with

gw.rowID = rID

without the need for Key-Value Coding methods.

更多推荐

无法使用类型(Int64,String)的参数列表调用setValue

本文发布于:2023-11-26 16:33:13,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   类型   列表   String   setValue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!