无法调用“保存”具有类型“(nil)”的参数列表

编程入门 行业动态 更新时间:2024-10-26 21:36:26
本文介绍了无法调用“保存”具有类型“(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)”的参数列表

本文发布于:2023-11-26 16:40:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634410.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   类型   列表   nil

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!