【问题标题】:XCode 6 Beta 6 Error in Beta 7 - Value of optional type not unwrappedXCode 6 Beta 6 Beta 7 中的错误 - 可选类型的值未解包
【发布时间】:2014-11-07 14:25:01
【问题描述】:

我一直在尝试做一个简单的 CoreData 任务,保存数据。我确定它在 Beta 6 中可以工作,但在更新到 Beta 7 后开始出现错误。

我想我必须添加“?”要么 '!'基于错误提示,但还不够聪明,无法弄清楚在哪里!

    @IBAction func saveItem(sender: AnyObject) {

    // Reference to App Delegate

    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate

    // Reference our moc (managed object content)

    let contxt: NSManagedObjectContext = appDel.managedObjectContext!
    let ent = NSEntityDescription.entityForName("List", inManagedObjectContext: contxt)

    // Create instance of our data model and initialize

    var newItem = Model(entity: ent, insertIntoManagedObjectContext: contxt)

    // Map our attributes

    newItem.item = textFieldItem.text
    newItem.quanitity = textFieldQuantity.text
    newItem.info = textFieldInfo.text

    // Save context

    contxt.save(nil) 
}

错误提示

Value of optional type 'NSEntityDescription?' not unwrapped; did you mean to use '!' or '?'

在线上

var newItem = Model(entity: ent, insertIntoManagedObjectContext: contxt)

每次我似乎已经清除错误并编译正常时,单击调试区域中显示的“保存”

fatal error: unexpectedly found nil while unwrapping an Optional value

【问题讨论】:

    标签: core-data swift xcode6 xcode6-beta7


    【解决方案1】:

    错误是相当微不足道的,这里没有太多要分析的。尝试改变这个:

    let ent = NSEntityDescription.entityForName("List", inManagedObjectContext: context)
    

    到这里

    let ent = NSEntityDescription.entityForName("List", inManagedObjectContext: context)!
    

    与往常一样,新手往往会忽略明显的迹象。该错误清楚地表明可选的类型为NSEntityDescription。并且鉴于在给定的代码中只实例化了这种类型的对象,因此无需天才就能猜出错误所在。

    Value of optional type 'NSEntityDescription?' not unwrapped; did you mean to use '!' or '?'
    

    另外,这里用来实例化 NSEntityDescription 对象的方法声明如下:

    class func entityForName(entityName: String, inManagedObjectContext context: NSManagedObjectContext) -> NSEntityDescription? 
    

    ...? 字符清楚地告诉我们此方法返回一个可选值。

    【讨论】:

      【解决方案2】:

      我假设 Model 初始化器签名是:

      init(entity: NSEntityDescription, insertIntoManagedObjectContext: NSManagedObjectContext)
      

      编译错误是因为NSEntityDescription.entityForName返回一个可选的,所以你必须解开它。

      至于运行时错误,我的猜测是contxt 为nil,而你在这里传递的是强制解包:

      let ent = NSEntityDescription.entityForName("List", inManagedObjectContext: contxt)
      

      为了使代码更安全、更清晰,我会明确地使用可选项:

      let contxt: NSManagedObjectContext? = appDel.managedObjectContext
      if let contxt = contxt {
          let ent: NSEntityDescription? = NSEntityDescription.entityForName("List", inManagedObjectContext: contxt)
      
          // Create instance of our data model and initialize
      
          if let ent = ent {
              var newItem = Model(entity: ent, insertIntoManagedObjectContext: contxt)
          }
      }
      

      并使用调试器和断点检查是否有任何提到的变量为零。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-29
        • 2014-10-13
        • 1970-01-01
        • 2015-10-04
        • 1970-01-01
        • 2014-08-10
        • 2014-10-24
        • 1970-01-01
        相关资源
        最近更新 更多