【问题标题】:Swift: exc_breakpoint (code=exc_arm_breakpoint subcode=0xdefe) on prepareForSegueSwift:prepareForSegue 上的 exc_breakpoint (code=exc_arm_breakpoint subcode=0xdefe)
【发布时间】:2015-02-07 09:07:39
【问题描述】:

由于某种原因,当到达 performSegueWithIdentifier 行时,我收到此错误。

我有这个代码:

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"

我也遇到同样的错误。

谁能告诉我这个错误是什么以及如何解决它。谢谢。

编辑:错误截图:

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    动态转换类无条件表示强制转换失败,因为无法将变量转换为另一种类型。

    在您的代码中,我只在这一行看到一个演员表:

    let destinationVC = segue.destinationViewController as brandsViewController
    

    这意味着目标视图控制器不是brandsViewController 的实例。

    解决问题:

    • 在界面生成器中检查目标视图控制器的自定义类属性是否正确设置为brandsViewController
    • 检查 segue 是否确实指向该视图控制器

    如果以上方法都不能解决问题,请在该行设置断点并检查目标视图控制器的实际类型。

    旁注:按照惯例,swift中所有类型名称都以大写字母开头,而函数、变量和属性则以小写字母开头。如果您想让您的代码对其他 swift 开发人员可读,我建议您坚持该约定(将 brandsViewController 重命名为 BrandsViewController

    【讨论】:

    • 谢谢,我是 Swift/Xcode 的新手,忘记添加 Segue。
    【解决方案2】:

    @antonios 的回答应该可以解决您的问题。中断是由于对象没有被强制转换(找到并分配)。

    只是一个旁注:这行会有一些问题:

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

    特别是如果您希望从中获取一个字符串并在 ViewController 之间传递它?

    将其转换为字符串,创建一个全局范围变量,然后将其分配给该变量以使用 - 这样处理起来会容易得多。

    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
    }
    

    【讨论】:

    • 这对我有很大帮助。
    猜你喜欢
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多