Search code examples
swiftnslayoutconstraint

What is it called when you bind a method and variable of a class in swift?


I'm following a book's tutorial on programmatic constraints and decided to read Apple's programming guide afterwards. I saw the following line of code in Apple's guide which is using a method and a variable in one line of code:

// Pin the leading edge of myView to the margin's leading edge
myView.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true

I didn't know that it's possible to condense code in this way. Is there a term for this?

Apologies for sounding like a noob but I would like to know.


Solution

  • This is normal. If you read the documentation of constraint(equalTo:) method, you'll find that it returns an object of type NSLayoutConstraint which you can use like any other object ==> You can use its properties and methods in the normal way.

    This concept is called Chaining, and it's not related to Swift only.