Accessing instance variables: Xcode warnings


Carl Hoefs
 

I have an ObjC class that has an instance variable "property" declared as such:

@property (nonatomic,retain,nonnull) NSString *mType;

In a instance method of this class, I can refer to mType in various ways, with warnings:

mType "Instance variable 'mType' is being directly accessed"
_mType "Use of undeclared identifier '_mType'; did you mean 'mType'?"
self.mType (No warning, but is very slow if used in a loop. Needed for blocks)


Q1: "Direct" access to an instance variable bypasses its setter/getter methods, but otherwise what is wrong with it?

Q2: Sometimes using the underscore form, _mType, quiets the direct access warning; other times Xcode acts like it doesn't know what I'm referring to. What is the underscore intended for?

Yes I can turn the Xcode warnings off by deleting -Weverything, but I want to do the right thing...

-Carl

Join xcode@apple-dev.groups.io to automatically receive all group messages.