Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

ios - Cannot invoke setValue with an argument list of type (Int64, String)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...