What does .text and the dot mean in Swift?

I am currently coding my own app and I was looking at a coding video for help and the person in the video was using .text and some other things that had dots in front of them like .color and .shape, which were variables declared and initialized already. I was wondering if anybody has a clear definition on what the .text means or what it does and what the overall dot means whenever you use it.


Thanks to anybody who has a solution!

'dot syntax' allows you to access properties; started with Obj-C


See:

Dot Syntax Is a Concise Alternative to Accessor Method Calls


https://stackoverflow.com/questions/1573278/when-to-use-brackets-and-when-to-use-the-period-in-objective-c


'text' or 'color' is simply a name assigned to that property.

KMT is correct in that dot syntax is used to access properties. For example, if you have a

UITextField
called
myTextField
and code like this:
myTextField.textColor = UIColor.red

then:

  • The first dot tells Swift that you’re assigning to the

    textColor
    property of the
    myTextField
    object
  • The second dot says that you’re getting the

    red
    property of the
    UIColor
    class.

The situation with leading dots is more nuanced. You can rewrite the above line as:

myTextField.textColor = .red

In this case the

UIColor
can be omitted because Swift can infer that from the context. It knows that you’re assigning to
textColor
, which is a
UIColor
, and then
.red
refers to
UIColor.red
.

If you have any other cases that you still don’t understand, please post some code snippets and we can take things from there.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
What does .text and the dot mean in Swift?
 
 
Q