0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Laurence Wingo
  • 3,912
  • 7
  • 33
  • 61
  • What part of that line are you referring to? Using methods and variables is extremely common. In fact, most lines of code involve calling methods on a variable. – rmaddy Nov 11 '17 at 23:25
  • Sorry, I was referring to the isActive property being set together with the leadingAnchor or constraint method. @rmaddy – Laurence Wingo Nov 11 '17 at 23:30

1 Answers1

2

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.

Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36