命令由于信号失败:分段错误:11由于TableViewController? [英] Command failed due to signal: Segmentation fault: 11 due to TableViewController?

查看:124
本文介绍了命令由于信号失败:分段错误:11由于TableViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在"DictionaryTableViewController:UITableViewController"类中,定义以下变量:

In my 'DictionaryTableViewController: UITableViewController' class I define the below variables:

var dataObj : [String: AnyObject]!
var letters : Int!
var currentSection : Int!;
var currentRow : Int!

在"viewDidLoad"中,我有:

In 'viewDidLoad' I have:

    let jsonUrl = NSBundle.mainBundle().URLForResource("dictionary", withExtension: "json")
    var data = NSData(contentsOfURL: jsonUrl!)
    func dataReturn(object: [String: AnyObject]) {
        dataObj = object
        letters = object["collection"]!.count
    }

    do {
        let object = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
        if let dictionary = object as? [String: AnyObject] {
            dataReturn(dictionary)
        }
    } catch {
        // Handle Error
    }

这是我从NSBundle中提取的JSON文件的基本结构

This is the basic structure of the JSON file that I am pulling in from NSBundle

    {
        "collection": [{
            "letter": "A",
            "words": [{
                "word" : "Apple",
                "definition" : "Tasty fruit"
            }]
        },{
            "letter": "B",
            "words": [{
                "word" : "Banana",
                "definition" : "Meh, not bad."
            }]
        },{
            "letter": "C",
            "words": [{
                "word" : "Carrots",
                "definition" : "hooock twooo!"
            }]
        }] 
    }    

现在,当我对tableView单元格进行样式设置时,我认为这是导致此错误的原因:

So now when I go style the tableView cell I believe that is what is causing this error:

命令由于信号失败:分段错误:11

Command failed due to signal: Segmentation fault: 11

  1. 在/Users/me/Desktop/.../DictionaryTableViewController.swift:70:14上为'tableView'发出SIL时

作为警告,我只是在更新到xcode 7.3后才遇到此问题. 7.2中不存在此问题

As a warning I am just getting this issue after updating to xcode 7.3. This issue was not present in 7.2

第70行从出现错误的位置开始变为从以下位置读取:

Line 70 from the where the error says to becoming from reads:

Line 70: override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
        //let currentSelection = dataObj["collection"]![indexPath.section]["words"]!![indexPath.row]!
        let currentSelection = dataObj["collection"]![indexPath.section]["words"]!![indexPath.row]!!!
        cell.textLabel?.text = currentSelection["word"] as? String
        cell.detailTextLabel?.text = currentSelection["definition"] as? String
        return cell
    }

注意:上面代码中的注释行在xcode 7.2中正常工作. Xcode7.3给了我一个语法错误(下标的模棱两可使用).注释行下方的代码是我的更改,不会产生语法错误.这是引起我问题的原因吗?我在这里真的很茫然,似乎找不到答案.任何帮助表示赞赏!

Note: The commented out line in the above code was working correctly in xcode 7.2. Xcode7.3 gave me a syntax error (Ambiguous use of subscript). The code just below the commented out line are my changes that produce no syntax errors. Is this what is causing my issue? I am really at a loss here and can't seem to find an answer. Any help is appreciated!

推荐答案

出于调试目的,我将使用所有强制展开的可选选项将该行分解为一系列的警卫声明,如下所示:

For debugging purposes, I would break down that line with all the force-unwrapped optionals into a series of guard statements like this:

guard let collection = dataObj["collection"] else { fatalError("collection is nil") }

guard let words = collection[indexPath.section]["words"] else { fatalError("words is nil") }

guard let word = words[indexPath.row] else { fatalError("word is nil") }

无论如何,这样的事情都会告诉您这些强制展开的可选对象中的一个是否引起了问题.

Something like that, anyway, will tell you if one of those force-unwrapped optionals was causing the problem.

这篇关于命令由于信号失败:分段错误:11由于TableViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆