如何将数据保存到模型coredata

时间:2016-08-13 23:47:17

标签: ios arrays swift uitableview core-data

我想在点击按钮时将数据保存到核心数据这里是我的动作按钮代码let app = UIApplication.sharedApplication().delegate as! AppDelegate let context = app.managedObjectContext let entity = NSEntityDescription.entityForName("Cart", inManagedObjectContext: context) let cart = Cart(entity: entity!, insertIntoManagedObjectContext: context)但现在唯一的问题是我不知道如何保存这里是我的代码来解析json

      Alamofire.request(.GET, url!, headers: headers).responseJSON { (response) in
        let result = response.result

        if response.result.isSuccess{
            let jsonObj = JSON(result.value!)
            if let x = jsonObj["Items"].array {
                x.forEach
                    {
                        if let uw = ($0["name"]).string{
                            print(uw)
                            let qw = ($0["image"]).string
                            if let rw = ($0["price"]).int{
                                if let hu = ($0["id"]).int{
                                print(hu)
                                let foo = Rest(name: uw, imageUrl: qw!, price: "₦\(rw)")
                                    self.rest.append(foo)

                                }


                            }

                        }

                }

                dispatch_async(dispatch_get_main_queue()) {
                    self.tableView.reloadData()
                    actInd.stopAnimating()

                }

            }

        }

所以我有一个名为rest的数组,这是我想要获取tableview数据的地方。我想在这里保存每个单元格的信息,这里是应用程序Screenshot的屏幕截图

因此,当点击确定按钮时,它会将项目保存到coredata 这是我的代码,你需要code

2 个答案:

答案 0 :(得分:2)

试试这个,根据你的代码进行调整。

在您拥有的部分(...让app = ...)中将其替换为:

 let app2 = UIApplication.sharedApplication().delegate as! UIApplicationDelegate
        if let context2 = app2.managedObjectContext {
            let cart2 = NSEntityDescription.insertNewObjectForEntityForName("Cart", inManagedObjectContext: context2) as! Cart
            cart2.name = self.res.name
            cart2.price = self.res.price
            cart2.url = self.res.imageUrl
            cart2.id = ""
            // Perform a save operation
            do {
                try context2?.save()
            } catch {
                let saveError = error as NSError
                print(saveError)
            }
      }

让我知道这是否有效。

干杯

答案 1 :(得分:1)

这里只是一个例子,当您想要在coredata中保存数据时。您必须创建实体/模型以及核心数据元素的所有设置。有关详细信息,请参阅https://www.raywenderlich.com/115695/getting-started-with-core-data-tutorial

  func saveName(name: String) {
  //1
  let appDelegate =
  UIApplication.sharedApplication().delegate as! AppDelegate

  let managedContext = appDelegate.managedObjectContext

  //2
  let entity =  NSEntityDescription.entityForName("Person",
    inManagedObjectContext:managedContext)

  let person = NSManagedObject(entity: entity!,
    insertIntoManagedObjectContext: managedContext)

  //3
  person.setValue(name, forKey: "name")

  //4
  do {
    try managedContext.save()
  //5
    people.append(person)
  } catch let error as NSError  {
      print("Could not save \(error), \(error.userInfo)")
  }
}
相关问题