Re: Accessing instance variables: Xcode warnings


Alex Zavatone
 

_mType is the internal class scoped copy of it.

self. is safest. It’s wrapped with the get and set accessors.

On Dec 11, 2020, at 1:02 PM, Carl Hoefs <newslists@autonomy.caltech.edu> wrote:

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.