2015-02-07 39 views
3

出於某種原因,當達到performSegueWithIdentifier行時出現此錯誤。Swift:exc_breakpoint(code = exc_arm_breakpoint subcode = 0xdefe)onForForSegue

我有這樣的代碼:

if let storedAPIKeychain: AnyObject = dictionary.objectForKey("api_key") { 

      println(storedAPIKeychain) 

      //This is the line that causes the problems. 
      performSegueWithIdentifier("skipBrandSegue", sender: self) 

     } 

的println()工作正常,並輸出正確的信息。

我試圖通過storedAPIKeychain與SEGUE一起:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { 
    if segue.identifier == "skipBrandSegue" { 

     // Create a new variable to store the instance of the next view controller 
     let destinationVC = segue.destinationViewController as brandsViewController 
     destinationVC.storedAPIKey = storedAPIKeychain! 

    } 
} 

而且我認爲他可能會出現的問題。然而,當我將該行更改爲:

destinationVC.storedAPIKey = "someAPIplaceholder" 

我也遇到同樣的錯誤。

有人可以告訴我這個錯誤是什麼以及如何解決它。謝謝。

編輯:錯誤的屏幕截圖: Xcode DebugError

回答

4

的動態轉換類無條件表明被迫施放失敗,因爲變量不能轉換爲另一種類型。

在你的代碼中,我看到一個投只有在這行:

let destinationVC = segue.destinationViewController as brandsViewController 

這意味着目標視圖控制器不是brandsViewController一個實例。

要解決此問題:

  • 檢查接口生成器,爲目標視圖控制器的自定義類屬性正確設置爲brandsViewController
  • 檢查SEGUE實際上指向一個視圖控制器

如果以上都沒有解決該問題,請在該行設置斷點並檢查目標視圖控制器的實際類型。注意:按照慣例,在swift中,所有類型名稱都以大寫字母開頭,而函數,變量和小寫屬性。如果您想讓您的代碼對其他快速開發人員可讀,建議您遵循該慣例(將brandsViewController重命名爲BrandsViewController

+0

謝謝,我是Swift/Xcode的新手,我忘了添加Segue。 – DannieCoderBoi 2015-02-08 05:40:25

4

@antonios答案應該可以解決您的問題。中斷是由於對象未被投射(找到並分配)。

只是一個側面說明:你將有一些問題,與這條線:

if let storedAPIKeychain: AnyObject = dictionary.objectForKey("api_key") 

特別是如果你希望從中得到一個字符串並傳遞ViewControllers之間?

把它作爲一個字符串強制轉換,創建一個全局作用域變量,然後將其賦值給該變量使用 - 然後處理會更容易。

var globalVariable = "" //add this line at the top, just before your class declaration. 

if let storedAPIKeychain = dictionary.objectForKey("api_key") as? String { 
    self.globalVariable = storedAPIKeychain 
} 
+0

這幫了我一大堆。 – DannieCoderBoi 2015-02-08 05:40:43

相關問題