ios - swift:线程1:exc_bad_instruction(code = exc_1386_invop,subcode = 0x0)

标签 ios swift xcode7

我不知道为什么我总是遇到这个错误!

再次!

http://i.imgur.com/Fe7GodI.png

顺便说一句,我使用xcode 7.3

如果这样的代码,不会出错

 case 2:
            self.recipesImageView.image = UIImage(named: noodles.image)
            title = self.noodles.name

`

但是如果这样的代码,“面条”就会变成“小吃”
case 2:
            self.recipesImageView.image = UIImage(named: snacks.image)
            title = self.snacks.name

然后将是错误:

“线程1:exc_bad_instruction(code = exc_1386_invop,subcode = 0x0)”

下面是我的整个代码
class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate  {

@IBOutlet var recipesImageView: UIImageView!
@IBOutlet var tableView:UITableView!
var recipes:Recipe!
var noodles:Recipe!
var snacks:Recipe!
var category: Int = Int()


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.


    switch self.category {
        case 0:
            self.recipesImageView.image = UIImage(named: recipes.image)
            title = self.recipes.name
        case 1:
            self.recipesImageView.image = UIImage(named: noodles.image)
            title = self.noodles.name
        case 2:
            self.recipesImageView.image = UIImage(named: snacks.image) //<------this line will be error
            title = self.snacks.name
        case 3:
            self.recipesImageView.image = UIImage(named: noodles.image)
            title = self.noodles.name
        case 4:
            self.recipesImageView.image = UIImage(named: noodles.image)
            title = self.noodles.name
        default:
            self.recipesImageView.image = UIImage(named: recipes.image)
            title = self.recipes.name
    }

    self.tableView.backgroundColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.5)

    //刪除多餘的隔線
    self.tableView.tableFooterView = UIView(frame: CGRectZero)

    self.tableView.separatorColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 1)

    //title = self.recipes.name

    tableView.estimatedRowHeight = 36.0;
    tableView.rowHeight = UITableViewAutomaticDimension;

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 4
}



func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! DetailTableViewCell


    //cell.backgroundColor = UIColor.clearColor()
    if self.category == 0 {
        switch indexPath.row {
        case 0:
            cell.fieldLabel.text = "名稱"
            cell.valueLabel.text = recipes.name
            cell.amountLabel.text = ""

        case 1:
            cell.fieldLabel.text = "類型"
            cell.valueLabel.text = recipes.type
            cell.amountLabel.text = ""

        case 2:
            cell.fieldLabel.text = "收藏"
            cell.valueLabel.text = (recipes.isFavor) ? "Yes" : "No"
            cell.amountLabel.text = ""

        case 3:
            cell.fieldLabel.text = "食材"
            cell.valueLabel.text = recipes.material
            cell.amountLabel.text = recipes.amount
        default:
            cell.fieldLabel.text = ""
            cell.valueLabel.text = ""
            cell.amountLabel.text = ""
        }
    }

    if self.category == 1 {
        switch indexPath.row {
        case 0:
            cell.fieldLabel.text = "名稱"
            cell.valueLabel.text = noodles.name
            cell.amountLabel.text = ""

        case 1:
            cell.fieldLabel.text = "類型"
            cell.valueLabel.text = noodles.type
            cell.amountLabel.text = ""

        case 2:
            cell.fieldLabel.text = "收藏"
            cell.valueLabel.text = (noodles.isFavor) ? "Yes" : "No"
            cell.amountLabel.text = ""

        case 3:
            cell.fieldLabel.text = "食材"
            cell.valueLabel.text = noodles.material
            cell.amountLabel.text = noodles.amount
        default:
            cell.fieldLabel.text = ""
            cell.valueLabel.text = ""
            cell.amountLabel.text = ""
        }
    }

    if self.category == 2 {
        switch indexPath.row {
        case 0:
            cell.fieldLabel.text = "名稱"
            cell.valueLabel.text = snacks.name
            cell.amountLabel.text = ""

        case 1:
            cell.fieldLabel.text = "類型"
            cell.valueLabel.text = snacks.type
            cell.amountLabel.text = ""

        case 2:
            cell.fieldLabel.text = "收藏"
            cell.valueLabel.text = (snacks.isFavor) ? "Yes" : "No"
            cell.amountLabel.text = ""

        case 3:
            cell.fieldLabel.text = "食材"
            cell.valueLabel.text = snacks.material
            cell.amountLabel.text = snacks.amount
        default:
            cell.fieldLabel.text = ""
            cell.valueLabel.text = ""
            cell.amountLabel.text = ""
        }
    }

    if self.category == 3 {
        switch indexPath.row {
        case 0:
            cell.fieldLabel.text = "名稱"
            cell.valueLabel.text = noodles.name
            cell.amountLabel.text = ""

        case 1:
            cell.fieldLabel.text = "類型"
            cell.valueLabel.text = noodles.type
            cell.amountLabel.text = ""

        case 2:
            cell.fieldLabel.text = "收藏"
            cell.valueLabel.text = (noodles.isFavor) ? "Yes" : "No"
            cell.amountLabel.text = ""

        case 3:
            cell.fieldLabel.text = "食材"
            cell.valueLabel.text = noodles.material
            cell.amountLabel.text = noodles.amount
        default:
            cell.fieldLabel.text = ""
            cell.valueLabel.text = ""
            cell.amountLabel.text = ""
        }
    }

    if self.category == 4 {
        switch indexPath.row {
        case 0:
            cell.fieldLabel.text = "名稱"
            cell.valueLabel.text = noodles.name
            cell.amountLabel.text = ""

        case 1:
            cell.fieldLabel.text = "類型"
            cell.valueLabel.text = noodles.type
            cell.amountLabel.text = ""

        case 2:
            cell.fieldLabel.text = "收藏"
            cell.valueLabel.text = (noodles.isFavor) ? "Yes" : "No"
            cell.amountLabel.text = ""

        case 3:
            cell.fieldLabel.text = "食材"
            cell.valueLabel.text = noodles.material
            cell.amountLabel.text = noodles.amount
        default:
            cell.fieldLabel.text = ""
            cell.valueLabel.text = ""
            cell.amountLabel.text = ""
        }
    }


    return cell
}


//避免被前面一頁的隱藏導覽列所影響
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    self.navigationController?.hidesBarsOnSwipe = false
    self.navigationController?.setNavigationBarHidden(false, animated: true)
}

/*
 // MARK: - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
 // Get the new view controller using segue.destinationViewController.
 // Pass the selected object to the new view controller.
 }
 */

}

最佳答案

前任控制人

    if segue.identifier == "showRecipesDetail" {
       // var obj = segue.destinationViewController as! DetailTableViewCell
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let destinationController = segue.destinationViewController as! DetailViewController

            if segment.selectedSegmentIndex == 0 {
                destinationController.recipes = recipes[indexPath.row]
                destinationController.category = self.category
                print(category)
            }
            if segment.selectedSegmentIndex == 1 {
                destinationController.noodles = noodles[indexPath.row]
                destinationController.category = self.category
                 print(category)
            }
            if segment.selectedSegmentIndex == 2 {
                destinationController.noodles = snacks[indexPath.row]
                destinationController.category = self.category
                print(category)
            }
            if segment.selectedSegmentIndex == 3 {
                destinationController.noodles = noodles[indexPath.row]
                destinationController.category = self.category
            }
            if segment.selectedSegmentIndex == 4 {
                destinationController.noodles = noodles[indexPath.row]
                destinationController.category = self.category
            }
        }
    }

}

下面的代码是错误的
if segment.selectedSegmentIndex == 2 {
            destinationController.noodles = snacks[indexPath.row] <----this line is wrong
            destinationController.category = self.category
            print(category)
 }

该代码应为
destinationController.snacks = snacks[indexPath.row]

关于ios - swift:线程1:exc_bad_instruction(code = exc_1386_invop,subcode = 0x0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39846451/

相关文章:

ios - 如何像 actionSheet 一样呈现 UIViewController?

IOS 视频压缩 Swift iOS 8 损坏的视频文件

swift - 无法运行模拟器 操作无法完成。 (LaunchServicesError 错误 0。)

objective-c - XCode(Objective C)和C程序之间的进程间通信

ios - 有没有办法限制临时或调试 iOS 构建仅在特定时间范围内运行?

ios - 通过zip将图片添加到项目中

ios - 在 iOS objective-c 中从前台过渡到后台后继续调用 Web 服务

ios - 将字符串转换为运算符 - swift

c++ - 在 Xcode 控制台项目中创建和使用 C++ 库

ios - UIsearchbar 搜索电子邮件和用户名