【问题标题】:Error saving the core data保存核心数据时出错
【发布时间】:2016-07-08 21:14:18
【问题描述】:

我在尝试保存日期核心时出错。错误发生在我为该项目提供 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{

    }



}

【问题讨论】:

标签: ios core-data swift2


【解决方案1】:

您需要将 id 包装在一个 NSNumber 中,以便与方法 setValue 一起使用它

试试:

let id = dictionary!["id"] as? NSNumber

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 2021-07-24
    相关资源
    最近更新 更多