Crashing Switching to Detail View


#1

My project builds and runs properly but crashes when going into the detail view. If I comment out the code that sets up the imageView constraints it works properly. I can’t for the life of me figure out why! I’ve compared my code against the one provided in Solutions and nothing seems to be incorrect. I’ve even tried using the xib from the Solutions and it still crashes:

2014-06-18 14:45:11.748 Homepwner[86124:60b] *** Assertion failure in -[NSLayoutConstraint constant], /SourceCache/Foundation_Sim/Foundation-1047.25/Layout.subproj/NSLayoutConstraint.m:601
2014-06-18 14:45:11.756 Homepwner[86124:60b] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘(null)’

Any suggestions on where to start looking for this error would be greatly appreciated.

David


#2

Make sure you are typing this

[self.view addConstraints:horizontalConstraints];
[self.view addConstraints:verticalConstraint];

instead of this

[self.view addConstraint:horizontalConstraints];
[self.view addConstraint:verticalConstraint];

The difference is the word addConstraint in the second example (singular - expects a NSLayoutConstraint pointer) vs addConstraints (plural - expects an NSArray pointer) in the first example.


#3

Thanks for the reply. I’ve taken another look at the code and resolved the issue, however I’m confused on why this would be the case.

  1. I had pluralized verticalContraints for the array name and when referencing to add to the view.
  2. I renamed as verticalContraint for the array name and adding to the view, to match your suggestion, and it built and was able to get into detail view without crash.

Then to see if I could replicate it, I put it back to the pluralized version, and it builds and goes into detailView without crashing!

Maybe a bad reference to the array? So bizarre.


#4

Glad it works now! Actually, I meant to write:

[self.view addConstraint:horizontalConstraints];
[self.view addConstraints:verticalConstraints];

The name of the Array pointer passed as a parameter doesn’t matter, as long as it matches the name of the array declaration. The problem you had before was likely caused by the name of the method you were using. This is why:

addConstraint: expects a [color=#FF0000]NSLayoutConstraint[/color] object. Now you didn’t declared a NSLayoutConstraint object, instead you decalred (as it shows in the book) a [color=#FF0000]NSArray[/color].

addConstraints: expects a [color=#FF0000]NSArray[/color] which is how you declared your constraints (both horizontalConstraints and verticalContraints) as it shows in the book.

Since you were using [color=#FF0000]addConstraint:[/color] and passing an Array as the parameters, the application crashed because once you switch to the detailViewController, xcode checks (at runtime) that the parameter your passed to the [color=#FF0000]addConstraint:[/color] method is an object of the right class by passing something like this behind the scenes:

So two different methods with very similar names, which is where the confusion occurs. I made the same mistake and it took me a bit to figure out it was a typo in the method name (that extra s I typed).