Variable Bindings

When working with visual constraints, the layout system needs to be able to associate view names like leftLabel and rightLabel with the actual views they represent. Enter variable bindings, via a handy macro defined in NSLayoutConstraint.h, which is part of the UIKit headers.

The NSDictionaryOfVariableBindings() macro accepts an arbitrary number of local variable arguments. As you can see in the earlier example, these need not be terminated with a nil. The macro builds a dictionary from the passed variables, using the variable names as keys and the actual variables as values. For example, this function call:

NSDictionaryOfVariableBindings(leftLabel, rightLabel)

builds this dictionary:

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.