ObjectiveC: where to declare private instance properties?
Asked Answered
S

4

12

I have the following class interface:

@interface MyClass : NSObject

@property int publicProperty;

@end

then the implementation:

@interface MyClass() // class extension

- (void)privateMethod; // private methods

@end

@implementation MyClass {
    int _privateProperty;
}

@property int privateProperty = _privateProperty;

@end

this is what the Apple guy showed in WWDC, but is there any reason for NOT putting _privateProperty in class extension like:

@interface MyClass() // class extension
{
    int _privateProperty;
}

- (void)privateMethod; // private methods

@end

Thanks!

Something answered 3/7, 2012 at 4:9 Comment(2)
You might find many good answers to your question here: stackoverflow.com/search?q=private+propertyExtensity
possible duplicate of How to make a private property?Extensity
W
9

You dont have to declare your ivars in both the interface and the implementation.Because you want to make them private you can just declared them in the implementation file like so:

@implementation {

int firstVariable;
int secondVariable;
...
}
//properties and code for  your methods

If you wanted to, you can then create getter and setter methods so that you can access those variables.

The person you spoke to was right, though there is not any reason why you would NOT declare them the same way in the interface. Some books actually teach you that the @interface shows the public face of the class and what you have in the implementation will be private.

Wolverhampton answered 3/7, 2012 at 6:27 Comment(0)
P
12

I usually "force" private with an extension in the implementation

In your header

@interface MyClass : NSObject
{
}

@property (nonatomic, assign) int publicProperty;

@end

In your implementation file:

@interface MyClass ()
@property (nonatomic, assign) int privateProperty;
@end


@implementation MyClass
@synthesize privateProperty;
@synthesize publicProperty;

@end
Paresh answered 3/7, 2012 at 13:27 Comment(0)
W
9

You dont have to declare your ivars in both the interface and the implementation.Because you want to make them private you can just declared them in the implementation file like so:

@implementation {

int firstVariable;
int secondVariable;
...
}
//properties and code for  your methods

If you wanted to, you can then create getter and setter methods so that you can access those variables.

The person you spoke to was right, though there is not any reason why you would NOT declare them the same way in the interface. Some books actually teach you that the @interface shows the public face of the class and what you have in the implementation will be private.

Wolverhampton answered 3/7, 2012 at 6:27 Comment(0)
E
0

Do you mean you want to declare private instance variables?

You can do this:

@interface MyClass()
{
 @private //makes the following ivar private
   int _privateProperty;
}
Emmie answered 3/7, 2012 at 4:12 Comment(2)
Yes but I don't want to make it ivar, it is just a property which can be accessed by self.privateProperty using accessor, and not exposed in @interface.Something
You can't do it by this @property int privateProperty = _privateProperty;, you have to do it in the implementation file.Emmie
M
0

With the "modern runtime" (64-bit MacOS post-10.5 and all versions of iOS), you don't need to declare instance variables at all.

// MyClass.h
@interface MyClass : NSObject

@property int publicProperty;

@end


// MyClass.m
@implementation MyClass

@synthesize publicProperty = _privateProperty;  // int _privateProperty is automatically synthesized for you.

@end
Multiflorous answered 3/7, 2012 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.