我有一些代码从 segue 接收值并用我拥有的索引号替换数组的某个元素。
变量的初始化是:
var noteTitles: [String] = ["Sample Note"]
var noteBodies: [String] = ["This is what lies within"]
var selectedNoteIndex: Int!
var newTitle: String!
var newBody: String!
我有一个 segue 使最后 3 个值成为我希望它们成为的值。
在 viewDidLoad() 下,我有这个:
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
}
我的日志显示:
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
谁能告诉我我做错了什么?