Trying to figure what this means

I was working through some lessons on swift (I come from a mainframe background) and I grasp (somewhat) the basics of classes, but this line of code has me a little stumped:


self.resultsField.StringValue = result.description


resultsField is a texbox.

result is a var type of Double, so am I to assume that description is a string version of the variables value?


Also, what is the resultsField using the "self" before it? Why wouldn't this work as well:


results.Field.StringValue = result.description

Accepted Reply

"self." indicates that whatever is after it is a property of the instance of the class where the code is executing. You are correct, in Swift the "self." is usually optional (I think there are some times it is required, but I can't remember when off the top of my head). There has been at least one discussion on this forum about whether self being optional is a "good thing" or a "bad thing"🙂.


You are also correct about the description property; it is defined as "A textual representation of 'self'"

Replies

I mistyped the last line, it should have been :


resultsField.StringValue = result.description

"self." indicates that whatever is after it is a property of the instance of the class where the code is executing. You are correct, in Swift the "self." is usually optional (I think there are some times it is required, but I can't remember when off the top of my head). There has been at least one discussion on this forum about whether self being optional is a "good thing" or a "bad thing"🙂.


You are also correct about the description property; it is defined as "A textual representation of 'self'"

Thanks...

If I remember well, self. is notably mandatory in a closure. And in some other cases where it could be unclear whether we mean the instance property or something else