线程1:EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子代码= 0x0)

时间:2016-04-26 17:07:29

标签: swift exc-bad-instruction

如此接近实际让这个应用程序移动。还有一个问题。现在,我的应用程序将运行,当下一个视图控制器试图显示时,它会给我一个错误。我正在展示一个带引号和作者的ViewController。我收到此错误

  

主题1:EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子代码= 0x0)

输出如下:

  

致命错误:在打开可选

时意外发现nil

这是带错误的代码:

import Foundation
import UIKit
import Social

class businessQuote: UIViewController {

//============================//
//********** Outlets *********//
//============================//

let utility = Utility()
@IBOutlet weak var quoteDisplay: UILabel!
@IBOutlet weak var authorDisplay: UILabel!
  //GET BACK TO THIS

//============================//
//********** General *********//
//============================//

let date = NSDate()
var Author: String = ""
var Quote: String = ""

override func viewDidLoad() {
    super.viewDidLoad()

    // Generates Random Number
    func randomNumber(arrayLength: Int) -> Int {
        let unsignedArrayCount = UInt32(arrayLength)
        let unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
        let randomNumber = Int(unsignedRandomNumber)


        return randomNumber
    }

    // Importing Quotes plist File
    let businessQuote = ImportList(FileName: "BusinessList")

    // Selects Quote
    let chosenQuote: String = businessQuote.array[randomNumber(businessQuote
        .count())] as! String
    let chosenAuthor = ((businessQuote.dict[chosenQuote]))! as String //This Is Where the error is


    // Assigns Quote & Author to IBOutlet
    Author = chosenAuthor
    Quote = chosenQuote

    quoteDisplay.text = Quote
    authorDisplay.text = Author.uppercaseString

}

}

Here's a screenshot

提前谢谢!

1 个答案:

答案 0 :(得分:3)

由于使用了力量展开,它正在崩溃。

使用 IF LET 检查数据的最佳做法是否存在,因为IF LET会这样做(展开)。

This way Try once : 
if let mStringvalue = businessQuote.dict[chosenQuote]{
  print(mStringvalue)
}