Set attribute text to UIButton

I am trying to set an attributed string to a button.

The definition of this function in swift is

func setAttributedTitle(_ title: NSAttributedString!,
               forState state: UIControlState)

so I thought that I would have to type it as

myButton.setAttributedTitle(title:attrString, forState: UIControlState.Normal)

but correct

myButton.setAttributedTitle(attrString, forState: UIControlState.Normal)

Why should I put forState:, but not title? I do not understand this.

And by the way, what is the meaning of this underline in the definition of func?

+4
source share
1 answer

Since parameter methods are fast. I emphasize methods because this is not done for bare functions, i.e. funcdefinitions outside the scope of the class.

, .

Swift Objective-C. Objective-C, Swift , , , , incrementBy Counter. . Swift , , .

, Swift , . , , Objective-C, .

: . , , , .

class Foo {
    func bar(a: String, b: Int) {}
    func baz(a: String, _ b: Int) {}
}

:

var f = Foo()
f.bar("hello", b: 1)
f.baz("hello", 1)

_ , b ,

f.baz("hello", b: 1)
+3

Source: https://habr.com/ru/post/1544031/


All Articles