Xcode why use self
What is "self" used for in Swift? Ask Question. Asked 7 years ago. Active 1 year, 7 months ago. Viewed 61k times. I am new to Swift and I'm wondering what self is used for and why. Improve this question. Richard Slater 6, 4 4 gold badges 47 47 silver badges 79 79 bronze badges. Well, it is the same as this in Java, this in Javascript, self in Objective-C.
You need to get familiar with that concept Are you really asking what self means or why you can sometimes often leave it out as it will be implied? Add a comment. Active Oldest Votes. Improve this answer.
David Gomez David Gomez 2, 2 2 gold badges 16 16 silver badges 27 27 bronze badges. Why is it deemed necessary in closures when there is no hiding happening? Concerning comments about closures: self if used for the code to be readable, to make the properties usage explicit. In Java this could be used in the same way for the same reason.
While I respect Ray Wenderlichs team very much for their work, I think they are not the top reference for clean code. In general I would recommend to use self to clarify on the first glance, if code is local or not. You shouldn't write code just for clarity, use documentation instead. Show 3 more comments. Leo Dabus Leo Dabus k 54 54 gold badges silver badges bronze badges. In what situations it's necessary to use it It is necessary to use it only when the name of a local variable overshadows the name of a property.
However, as a matter of style and readability , I always use it: I use it with property names, because otherwise I am left wondering what this variable is since it is neither locally declared nor an incoming parameter. This is why we need self. Is it possible to get rid of self? As mentioned in the introduction, Swift encourages you to omit the self keyword whenever possible.
In the previous example, it is recommended to remove self from isDayForWalk method. This makes the method shorter:. Inside the initializer init windSpeed:chanceOfRain: the parameters and structure properties have the same names windSpeed and chanceOfRain. Contrary to previous case, now you cannot remove self keyword, because it would create an ambiguity between the parameter and property names. It's an ambiguity, so Swift plainly decides that you mean only the parameters.
As result an error is thrown: cannot assign to value: 'windSpeed' is a 'let' constant. There were plenty of discussions about the obligatory usage of self to access properties, or contrary to skip self. The obligatory usage of self brings the benefits of consistency and favors clarity over brevity. You can clearly see the difference between the instance properties that are prefixed with self. In my opinion, when you have troubles to distinguish instance properties from local variables within a method: you have a different, deeper problem.
When a structure or class has an excessive number of properties so called God object , you're probably breaking the Single responsibility principle. If a method uses a big number of these properties and declares correspondingly many local variables, then such method does too many things.
Using an explicit self to distinguish somehow properties from variables is a temporary workaround for a bad code. Now imagine a well designed structure or class, which has a single responsibility.
It contains only strictly necessary properties. A well written method is performing one determined action and as result its code is simple and obvious. Surely you don't have the problem to distinguish local variables from instance properties in such a method. The usage of self may be even discouraged, since your adding to obvious code redundant explanations. So design your classes and structures well, and don't let the methods grow to thousands of lines of code. Then you can omit self without difficulties, and your code becomes even more expressive and concise.
Let's define following Const structure with type properties minLimit , maxLimit and type method getLimitRange :. This is probably a basic question, but I'm still on they of learning the basics of swift, classes, the different methods etc. Thanks in advance! Asked by aeum Copy to clipboard Share this post. Copied to Clipboard. Classes, structures, and enumerations can all define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type.
Classes, structures, and enumerations can also define type methods, which are associated with the type itself. Type methods are similar to class methods in Objective-C. The fact that structures and enumerations can define methods in Swift is a major difference from C and Objective-C.
In Objective-C, classes are the only types that can define methods. In Swift, you can choose whether to define a class, structure, or enumeration, and still have the flexibility to define methods on the type you create.
Instance methods are functions that belong to instances of a particular class, structure, or enumeration. Instance methods have exactly the same syntax as functions, as described in Functions.
You write an instance method within the opening and closing braces of the type it belongs to. An instance method has implicit access to all other instance methods and properties of that type.
An instance method can be called only on a specific instance of the type it belongs to. The Counter class defines three instance methods:. The Counter class also declares a variable property, count , to keep track of the current counter value. The same is true for method parameters, because methods are just functions that are associated with a type. Every instance of a type has an implicit property called self , which is exactly equivalent to the instance itself.
You use the self property to refer to the current instance within its own instance methods.
0コメント