无法调用“保存”具有类型“(nil)”的参数列表 [英] Cannot invoke "save" with an argument list of type "(nil)'
问题描述
需要帮助打击代码。只是更新到Xcode 7 beta
need help with the blow code. Just updated to Xcode 7 beta
我得到以下错误无法调用保存参数列表类型(nil)'。这是在IOS 6
I get the following error "Cannot invoke "save" with an argument list of type "(nil)'". This was working in IOS 6
import UIKit
import CoreData
class ItemViewController: UIViewController {
@IBOutlet weak var textFieldDiveNumber: UITextField!
@IBOutlet weak var textFieldDiveDate: UITextField!
@IBOutlet weak var textFieldDiveLocation: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func saveTapped(sender: AnyObject) {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let contxt: NSManagedObjectContext = appDel.managedObjectContext
let en = NSEntityDescription.entityForName("List", inManagedObjectContext: contxt)
var newItem = Model(entity: (en)!, insertIntoManagedObjectContext: contxt)
newItem.divenumber = textFieldDiveNumber.text!
newItem.divedate = textFieldDiveDate.text!
newItem.divelocation = textFieldDiveLocation.text!
contxt.save(nil)
self.navigationController?.popToRootViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
推荐答案
自上星期一以来已经多次问过这个问题,标记重复的SE iOS应用程序是繁琐的,因此...
This has been asked several times since last Monday, but finding & marking duplicates from the SE iOS app is cumbersome, so...
在Swift 2中, NSManagedObjectContext
code> save()方法被标记为 throws
,所以你必须处理来自它的任何错误。 (您不会将错误指针作为参数传递。)
In Swift 2, NSManagedObjectContext
's save()
method is marked throws
, so you have to handle any error that comes from it. (And you don't pass an error pointer as a parameter.)
您的Swift 1代码忽略错误; Swift 2的等价是一个空的 catch
:
Your Swift 1 code is ignoring errors; the Swift 2 equivalent is an empty catch
:
do {
try context.save()
} catch {
// you can go about your business. move along.
}
忽略错误不是一个好主意。如果你不想通过在 catch
中做一些有用的事情来使用户可恢复错误,只是计划在错误时崩溃:
Ignoring errors isn't a great idea, though. If you don't want to make an error user-recoverable by doing something useful in that catch
, just plan to crash on error:
try! context.save()
这篇关于无法调用“保存”具有类型“(nil)”的参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!