Christopher Francisco
Christopher Francisco

Reputation: 16288

iOS Basics: private property, public setter/getter

I've been reading the tutorials and I'm right now designing the model's architecture, and since I'm new to Objective-C, I'm not sure if the standards are like Java where you have public setter/getter and private attribute. This is 1 question I'd like to ask.

If the standards are so, declaring private properties are done in the *.m file @interface, but how do I @syntetize a setter/getter and how do I call them from outside: is it like [object SetProperty:property] ?

If the standards are different, can I get an example of a model class?

Thanks in advance.

Upvotes: 1

Views: 2582

Answers (5)

MDB983
MDB983

Reputation: 2454

Understand that declaring a property causes the compiler to create accessor for you . so if i require a pseudo private property personally I declare it in the implementation, if i need pseudo public property i declare it in the header. public getter / private setter can be handled as indicated below. There is no need to create your own setters and getters prefer using an attribute as it saves writing setters/getters ;

in the header (.h)

  @interface Person : NSObject
     @property (nonatomic, readonly) NSString *fullName;
  @end

in the implementation file (.m)

#import "Person.h"

@interface Person()
@property (nonatomic, readwrite) NSString *fullName;
@end

@implementation Person

 ... whatever this class does

// self.fullName = @"John Doe";

@end

Upvotes: 0

Starscream
Starscream

Reputation: 1148

Well it is true that Objective-C uses another terminology than most of the other languages like Java. If I get what you're asking, if you want a property to be directly available outside the class, the property must be declared in the .h file. However if you want to hide the implementation of your code, you can declare a property in the .m file and provide setters/getters to the outside world just returning the information you want to be visible.

The @synthesize clause is to me a simpifier. By synthesizing a property the getter/setter will be automatically implemented and you don't need to do it yourself.

Does this answer your question ?

Upvotes: 0

Anoop Vaidya
Anoop Vaidya

Reputation: 46563

You can create a private property and create public setter/getter method of your own. From this method you can assign or retrieve the value back.

@interface Person : NSObject
-(void)setTheName:(NSString *)fullName;
-(NSString *)theName;
@end

Implementation file:

#import "Person.h"

@interface Person()
@property(atomic) NSString *fullName;
@end

@implementation Person

-(void)setTheName:(NSString *)fullName{
    self.fullName = fullName;
}

-(NSString *)theName{
    return self.fullName;
}

@end

In the above is private however you can check the selector still exists(but throws a warning)

if ([p respondsToSelector:@selector(setFullName:)]) {
    [p performSelector:@selector(setFullName:) withObject:@"Anoop"];
}
NSLog(@">>>> %@",[p theName]);

Output will be :

 >>>> Anoop

However it is seldom required to set any private property from outside. If that is the requirement we can make the property public.

Upvotes: 1

Caleb
Caleb

Reputation: 125037

A property is essentially a promise that a class provides certain accessor methods. For example:

@property(strong, nonatomic) Foo *foo;

is a promise that the class provides -foo and -setFoo: methods. So, if you want the accessors to be public, declare the property in your class's public interface (i.e. in the header file) and be done with it.

It's true that the instance variable that backs that property (_foo, unless you specify a different name) will then be accessible, but it's very poor form to access another object's instance variables directly. Many things in Objective-C are governed by convention and that's generally enough to avoid problems. Also, a given property doesn't have to be backed by any instance variable at all: a property like fullName might be computed from other properties like firstName and lastName, so there's good reason beyond mere convention for clients to avoid accessing ivars directly.

Upvotes: 3

tiguero
tiguero

Reputation: 11537

The common approach if you want to give access to your attribute is to use the keyword @property in the .h file of your class to define a property. This will automatically define a setter and a getter and you don't need to synthesise your property as of Xcode 4.4.

Your private attribute will be accessible within your .m file and will have the name of your property with "_" as a prefix by default.

Upvotes: 1

Related Questions