致命错误:意外地发现零,同时展开一个可选值(当添加到阵列) [英] fatal error: unexpectedly found nil while unwrapping an Optional value(When adding to a array)

查看:174
本文介绍了致命错误:意外地发现零,同时展开一个可选值(当添加到阵列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些code,从接收赛格瑞价值和替换,我有索引号数组的某一个元素。

I have some code that receives value from a segue and replaces a certain element of an array with the index number that I have.

变量的初始化:

var noteTitles: [String] = ["Sample Note"]
var noteBodies: [String] = ["This is what lies within"]
var selectedNoteIndex: Int!
var newTitle: String!
var newBody: String!

和我有一个SEGUE,使得最后的3个值,我想他们是。值​​

and I have a segue that makes the last 3 values the values that I want them to be.

viewDidLoad中()下,我有这样的:

under viewDidLoad(), I have this:

    if newTitle == nil && newBody == nil {

    }
    else {
        println("\(newTitle)")
        println("\(newBody)")
        println("\(selectedNoteIndex)")
        let realTitle: String = newTitle
        let realBody: String = newBody
        let realIndex: Int = selectedNoteIndex
        noteTitles[realIndex] = realTitle
        noteBodies[realIndex] = realBody
    }

我的日志显示如下:

My logs show this:

New Note Title
This is what lies within
nil
fatal error: unexpectedly found nil while unwrapping an Optional value

和我得到

Thread 1: EXC_BAD_INSTRUCTION(code=EXC_i385_INVOP,subcode=0x0)

就行了。

let realIndex: Int = selectedNoteIndex

谁能告诉我什么,我做错了吗?

Can anyone tell me what I'm doing wrong?

推荐答案

我得到这些错误的原因是因为在segueing返回到主视图,我没有使用正确的开卷SEGUE,而是用另一个节目SEGUE,其中擦除的有previously一直视图控制器内的所有数据。通过创建一个开卷SEGUE,我能够在SEGUE到详细视图和prevent之前保持值的误差。

The reason I was getting these errors is because while segueing back to the main view, I was not using the proper unwind segue, and instead using another show segue, which erased all data that had previously been within the view controller. By creating a unwind segue, I was able to keep the values before the segue to the detail view and prevent the error.

这篇关于致命错误:意外地发现零,同时展开一个可选值(当添加到阵列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆