Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, December 26, 2015

Is it a good way to access instance variable with self? If I use a lot

Is it a good way to access instance variable with self? If I use a lot


class Persion: NSObject {       var name = "John"         func greeting() {            let message = "Hello \(self.name)"            print(message)       }  }  

take a look In greeting method of Persion class. I'm using self.name that let me get clear "Im using instance variable now not local variable" Is it a good way to access instance variable with self? If I use a lot.

Answer by simon for Is it a good way to access instance variable with self? If I use a lot


In deed it is a proper way to access your variable.

Answer by Raphael Oliveira for Is it a good way to access instance variable with self? If I use a lot


According to the documentation:

In practice, you don?t need to write self in your code very often. If you don?t explicitly write self, Swift assumes that you are referring to a property or method of the current instance whenever you use a known property or method name within a method.

So in my opinion only use selfwhen needed to differentiate between a local and instance variable like self.property = property on a constructor.

Answer by Darko for Is it a good way to access instance variable with self? If I use a lot


No, use self only when you really have to.

  • Inside a closure - the compiler warns you anyway about it. This is to remind you that you could create a reference cycle between the closure and self.
  • If a passed parameter in a func has the same name as a property variable or constant on self then you also must apply self to resolve the ambiguity.

Another argument against using "self": If you use self all the time (without obvious reason) then you are going to forget the hidden danger of passing self and creating a reference cycle. It get's also way easier to do a text search in a class for "self" to find the possibillity of a reference cycle.

Apart from all the good reasons it is just reduntant - there is anyway color highlighting for properties in Xcode.

Answer by rob mayoff for Is it a good way to access instance variable with self? If I use a lot


The question of whether to use self always, or only when necessary, is a matter of opinion. There has been a lot of debate on the swift-evolution mailing list about whether Swift should require the use of self to access instance variables, and a formal proposal that it should. The proposal has not yet been decided on as of this writing, but will probably be decided on soon (after Apple staffers return from vacation).

https://www.google.com/search?q=swift-evolution+mandatory+self+site:lists.swift.org

https://www.google.com/search?q=swift-evolution+require+self+instance+site%3Alists.swift.org

Answer by 13th Ghost for Is it a good way to access instance variable with self? If I use a lot


It is preferred to use the self keyword which is more suited as in OOPS.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.