Search code examples
iosswift3visual-format-language

Why am i getting a SIGABRT error from using Visual Format?


I am using a book on how to make real life apps in swift3. I recently got an error from using Visual Format. It spaced my line out because I have show line numbers and the line of code I put didn't have a line number near it. Then when I pressed Run it gave me a SIGABRT Error. Why and how can I fix this. It didn't mention any errors in the book. If you want to know what book is it, its: Paul Hudson "Hacking with Swift". Please Help!

It gave me an error of Unterminated String literal.

Here's my code:

view.addConstraints(
    NSLayoutConstraint.constraints(
        withVisualFormat:"V:|
            [label1(labelHeight@999)]-
            [label2(label1)]-
            [label3(label1)]-
            [label4(label1)]
            [label5(label1)]->=10-|",
        options: [],
        metrics: nil,
        views: viewsDictionary
    )
)

Solution

  • You have not specified the metrics dictionary, yet you are trying to access labelHeight from it.

    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: 
    Encountered metric with name "labelHeight", but value was not specified in metrics or views dictionaries 
    V:|[label1(labelHeight@999)]-[label2(label1)]-[label3(label1)]-[label4(label1)][label5(label1)]->=10-| 
                          ^'
    

    Edit: as per rmaddy's suggestion, here's how one would fix it:

    let metrics = [
        "labelHeight": <#labelHeight#>,
    ]
    view.addConstraints(NSLayoutConstraint.constraints(
        withVisualFormat:"V:|[label1(labelHeight@999)]-[label2(label1)]-[label3(label1)]-[label4(label1)][label5(label1)]->=10-|",
        options: [],
        metrics: metrics,
        views: viewsDictionary
    ))
    

    Notes:
    1) the sample fix is broken over multiple lines merely for readability.
    2) <#labelHeight#> will be translated to a placeholder when copied to Xcode.