无法使用类型(Int64,String)的参数列表调用setValue [英] Cannot invoke setValue with an argument list of type (Int64, String)
问题描述
我已经检查了这里的答案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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!