Pépito
Pépito

Reputation: 57

Objective-C propreties and private variable

I'm a debutant to objective-c and I don't understand something. I practiced c++ so some practices are not instinctif ..

1)

@implementation Car {
    // Private instance variables
    double _odometer;
}

Why a private attribute is in the .m file ?

2)

@interface Car : NSObject {
    // Protected instance variables (not recommended)
}

@property (copy) NSString *model;

-(void)drive

a)It seems model is declared like an attribute, but why it's not recommended to do it in the @interface ?

b) why the drive method is not in the interface ?

3)

What if i'm not use function allocation for exemple for a NString and initialise it directly with @"..." ?

4)

I don't understand the difference between + and - before method declaration too..

Thanks in advance

Upvotes: 0

Views: 103

Answers (2)

gnasher729
gnasher729

Reputation: 52632

There's a general principle in programming that you should make the smallest possible amount of information available to the outside. Anything that's in the .h file, anyone in the world can see, access, and mess up. If there's something wrong with an _odometer and it is in the header file, you have to go through all of your source code to find if something is using it wrong. If it's only in the .m file, you know that if there's a problem, it is in the .m file.

The other reason to not make things public is that you are free to change them if nobody knows about them. For some reason, you decide next month that having _odometer wasn't a good idea in the first place. By now three programmers have changed a dozen files to use _odometer because it was there. So removing that _odometer is now a lot, lot of work. If it was never in the .h file, nobody is using it so you only need to change your own .m file.

Upvotes: 0

Avt
Avt

Reputation: 17053

1.>Why a private attribute is in the .m file

Why not? Everything that is declared inside *.m is private because *.m file could not be imported (included) somewhere. In objective C you can declare instance variables in both files - *.h and *.m

2.a Recommended way is to use accessors (@properties in Objective C). For each property setter, getter and instance variable are generated. Default names for

@property (copy)NSString *str;

are:

- (void)setStr:(NSString*)obj; // setter
- (NSString*)str; //getter
NSString *_str; //instance variable

You can modify accessors names

@property (copy, setter = NewSetterName, getter = NewGetterName )NSString *str;

and instance variable name (should be done @implementation section)

@synthesize str = _newStingInstanceVariable;

Ofcource you can rewrite setters and getters that are generated by default.

2.b Interface is everything between @implementation and @end. {} area after @interface - is a place were you can declare instance variables.

3.It is ok for NSStrings

NSString *str = @"some text";

but for most classes it will not work as you expect:

NSMutableString *str = @"some text"; 
// You will receive warning here! And str will be NSString, not NSMutableString

4."+" - class methods (static in C++), "-" - instance methods.

Upvotes: 2

Related Questions