保存核心数据时出错

时间:2016-07-08 21:14:19

标签: ios core-data swift2

我在尝试保存日期核心时出错。错误发生在我给setValue项的行中。 错误出现在" item.setValue(id,Forkey:" id") "有人可以告诉我可能是什么?感谢

json是请求的json对象

错误:参数标签'(_:,forkey :)'不匹配任何可用的重载

func saveJson(json: AnyObject){       

 do{

        let dictionary = try NSJSONSerialization.JSONObjectWithData(json as! NSData, options: .MutableContainers) as? [String:AnyObject]
        let name = dictionary!["name"] as? String
        let id = dictionary!["id"] as? Int
        let email = dictionary!["email"] as! String
        let password = dictionary!["password"] as! String
        let verifyCode = dictionary!["verify_code"] as! String

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext
        let entity = NSEntityDescription.entityForName("User", inManagedObjectContext: managedContext)
        let item = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
        item.setValue(name, forKey: "name")
        item.setValue(id, forkey: "id")
        item.setValue(email, forkey: "email")
        item.setValue(verifyCode, forkey: "verifyCode")
        let val = item.valueForKey("email") as! String

        print("Valorrr",val)

        do{
            try managedContext.save()

        }catch{
            print("error")
        }


    } catch{

    }



}

1 个答案:

答案 0 :(得分:0)

您需要在NSNumber中包含 id ,以便将其与方法let id = dictionary!["id"] as? NSNumber 一起使用

尝试:

Bundle bundle = getIntent().getExtras.