Command failed due to signal: Segmentation fault:

2019-09-10 04:29发布

问题:

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

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

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
    }

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!"
            }]
        }] 
    }    

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

Command failed due to signal: Segmentation fault: 11

  1. While emitting SIL for 'tableView' at /Users/me/Desktop/.../DictionaryTableViewController.swift:70:14

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

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
    }

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!

回答1:

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.



标签: json xcode swift