2

As Objective-C has evolved (I use it exclusively with xcode/ios for iPhone/iPad development), there seems to be many different ways you can layout your class instance variables. Is there a 'best practice' way that's become common consensus? (I realise Apple demo/example code is all over the place in terms of style)

In particular the idea of handling private variables. Here are a number of ways I've seen to manage some instance variables for a class (I've left out the interface/implementation for brevity) - and I'm not even including the use of underscore named synthesized properties.

.h

@property (nonatomic, strong) NSString *aString;

.m

@synthesize aString;

- (void)aMethod {
   aString = @"Access directly, but if I don't have custom getter/setters and am using ARC, do I care?";
   self.aString = @"Access through self";
}

Or this:

.h

@property (readonly) NSString *aString;

.m

@property (nonatomic, strong) NSString *aString;
...
@synthesize aString;

Or this:

.m

@interface aClass {
 NSString *aPrivateString;
}

- (void)aMethod {
  aPrivateString = @"Now I have to access directly, but is this atomic/nonatomic?";
}

I don't want this question to turn into a style argument, but it seems to me there should be a "if you're not doing something specific or complex or weird, use this method for defining your class variables" standard.

5
  • 1
    All access to ivars is non-atomic. When you make a property atomic and access it through the getter/setter, the synthesized methods add the atomicity. If you access an ivar directly, even if you set it as atomic in the property, the access will not be atomic. Global variables and statics are atomic by default (the compiler adds code, although you can disable this). Commented Jul 8, 2012 at 19:49
  • Apple's recommendations for ivar declaration and definition are pretty well outlined in TOCPL. Commented Jul 8, 2012 at 19:56
  • 1
    And just a note on nomenclature: a "class variable" is normally a variable associated with the class (single variable for each class) as opposed to instance variables that are associated with each instance (and can have different values for different instances). Commented Jul 9, 2012 at 6:58
  • Yeah thanks - I've updated the question. I wanted to keep it general, but I guess global class variable definition is a separate question! Commented Jul 10, 2012 at 14:56
  • @JasonCoco "Global variables and statics are atomic by default" Can you post a reference to documentation on this? Commented Jul 25, 2012 at 0:53

1 Answer 1

2

I'd prefer use the following convention for public and private ivars/properties:

.h

@property (nonatomic, strong) NSString *publicString;

.m

@implemention aClass 
{
  NSNumber *_privateNumber;
}
@synthesize publicString = _publicString;

This way everything starting with _ is an ivar (_publicString is the underlying ivar for the property, this prevents me from using publicString while I want to use self.publicString).

Sign up to request clarification or add additional context in comments.

6 Comments

And in fact, it's Apple's recommendation that you leave it out.
"you dont need to declare" - You need to tell the compiler it exists through, right? Are you saying you'd prefer to define it via @property ... *_privateString; ... @synthesize?
@vikingosegundo/Josh If you read carefully you'll notice that I do not create an explicit ivar for the property. Only for the private ivar. _privateString and _publicString are two different ivar's and have nothing to do with each other. If was just a demonstration how I handle public properties and private ivars.
You're correct that I did misread, but private ivars should be declared at the start of the @implementation block rather than the @interface.
@Josh I have updated the answer to make it more obvious that the private and public ivars are separate.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.