Does local tableView declaration hide instance variable?

I understand why I get a warning in the header when I define my own tableView property in my own class and then use the name of the local tableView table.

What I want to know, why I DO NOT get this warning when I get my class from a UITableViewController that has its own tableView property? Does the compiler / editor only look at my class, and not at the parent class?

+4
source share
3 answers

When you implement a method, parameters / local variables use the same namespace as instance variables. However, they do not share the same namespaces as the declared properties, which means that a class can declare a property named someData (or inherit from one of its superclasses), have an instance variable of a subdirectory with some other name, and to implement a method of this class may also have a parameter / local variable named someData - the compiler will not give a warning in this case.

I assume that you have a declared property called tableView , as well as an instance variable called tableView that is either explicitly declared in the interface or automatically created when the property is synthesized. In this case, if you define a method that takes a parameter named tableView or declares a local variable called tableView , this local declaration will hide the instance variable called tableView (but not the property).

In the case of the UITableViewController there is no instance variable named tableView . There is a declared property called tableView , which, since it will not be hidden by a local (variable) declaration in another namespace.

One simple fix to avoid compiler warnings is to specify an instance variable name. For example, an instance variable might be called _tableView , and the property would still be called tableView , but would be synthesized as @synthesize tableView = _tableView .

+23
source

Enter the exact code that generated the warning.

A "local declaration" usually implies that you have something like:

 - (void) foo { int thisIsTheNameOfAnInstanceVariable; } 

There are probably other permutations through which you could do this.

0
source

I'm not quite sure if I answer correctly, but if you want to access variables in superclasses (like UITableView, since your class gets the form), you should use the "I". then the variable name forms the superclass. Whenever you directly call variables, for example. 'myVariable', it will only search for local instances.

-1
source

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


All Articles