简体   繁体   中英

Naming ivar the same as the class name

I've never considered this a problem, until I ran the Xcode "Analyze" feature. Say I have a class called dude and I have created an instance of class and synthesized it so self.dude is available.

This works just fine. However, in the dealloc if I put:

 [self.dude release];

I get an Analyzer warning because really what it should be is:

[dude release];

However, this spawns another warning, as Analyzer thinks I am sending the release to the class name, not to the ivar.

Now, if the ivar is named something different than the class, there is no problem and [ivar release]; works without Analyzer warnings.

Should I take this as a general indication that it is not wise to name an ivar the same as a class name? It has never interfered with my product compilation or execution, but the Analyzer opens new questions about this.

You should start you class names with uppercase letters ( Dude ) and instance variables (and other variables for that matter) with lower case letters ( dude ). This avoids the problem entirely and you'd also be following the standard naming conventions for Objective-C.

It definitely is not a common practice to start a class name with a lower case letter. Objective-C/Cocoa convention is that you start a class name with a capital, and the method name and the instance variable name with a lower case letter.

Just follow the common practice and name your class Dude and name your ivar dude .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM