2016-04-26 77 views
1

如此接近實際讓這個應用程序移動。還有一個問題。現在,我的應用程序將運行,當下一個視圖控制器試圖顯示時它會給我一個錯誤。我用一個引用和一個作者來展示一個ViewController。我得到這個錯誤線程1:EXC_BAD_INSTRUCTION(code = EXC_1386_INVOP,subcode = 0x0)

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

輸出如下:

fatal error: unexpectedly found nil while unwrapping an Optional

這是錯誤的代碼:

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

預先感謝您!

+0

您的詞典元素爲nil –

回答

3

它由於使用武力解開它崩潰。

最佳實踐使用如果LET檢查數據是否存在,因爲IF LET做了它(解包)。

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

這確實解決了這個錯誤的問題,現在它說'selectedAuthor'是一個未解決的標識符。包含「Author = selectedAuthor」的行有錯誤。 –

+0

更新您的代碼accrodingly。 –

+0

感謝您的幫助。 –

相關問題