What is the best way to declare a property? - iphone

We declare properties using the #property keyword and synthesize it in the implementation file. My question is,
What if I declare a property using the #property keyword and also declare a variable in the interface block with the same name? For example, consider the following code,
Interface:
#interface myClass : NSObject {
NSString *myClass_name; // LINE 1
}
#property(nonatomic, retain) NSString *myClass_name; // LINE 2
#end
Implementation:
#implementation myClass
#synthesize myClass_name // LINE 3
#end
Declaring myClass_name in LINE 1 will make any problem? Like any reference problem or any unnecessary memory consumption problem?

No, in fact, declaring properties like that expects it. You could replace your declaration to:
#interface MyClass : NSObject {
NSString *ivar;
}
#property (nonatomic, retain) NSString *myClass_name;
#end
And then change your implementation to
#implementation MyClass
#synthesize myClass_name = ivar;
#end
(If you don't specify the = some_ivar, it will assume the ivar has the same name as the property.)
You always need to have the following lines:
Declaration of the property (Line 2)
Synthesization of the property (Line 3)
When you synthesize the property, if you do not specify which ivar to use (by using =ivar at the end), it will assume that there is an ivar with the same name as the property.

Declaring properties and synthesizing it will not create any reference problem in your case.
Doing this will create accessor and setter methods for your instance variable in your class.
If the variable names in the property and the one declared in the class, then the xcode will refer both as a single variable.
Line 3 and Line 4 are must. Line 1 is optiona

I got the following content from Apple's doc for Declared Properties. I am posting it here, so that it may be helpful for someone in future.
Runtime Difference
In general the behavior of properties is identical on all runtimes (see Runtime Versions and Platforms in Objective-C Runtime Programming Guide). There is one key difference: the modern runtime supports instance variable synthesis whereas the legacy runtime does not.
For #synthesize to work in the legacy runtime, you must either provide an instance variable with the same name and compatible type of the property or specify another existing instance variable in the #synthesize statement. With the modern runtime, if you do not provide an instance variable, the compiler adds one for you. For example, given the following class declaration and implementation:
#interface MyClass : NSObject {
float sameName;
float otherName;
}
#property float sameName;
#property float differentName;
#property float noDeclaredIvar;
#end
#implementation MyClass
#synthesize sameName;
#synthesize differentName=otherName;
#synthesize noDeclaredIvar;
#end
the compiler for the legacy runtime would generate an error at #synthesize noDeclaredIvar; whereas the compiler for the modern runtime would add an instance variable to represent noDeclaredIvar.

The following is the Object-oriented way:
DeclaringProperties.h
#interface DeclaringProperties : NSObject
// ivars and {} can be omitted
#property (nonatomic, readwrite, retain) NSString *normal;
#property (nonatomic, readwrite, retain) NSString *alias;
#property (nonatomic, readonly, retain) NSString *readonly;
- (id) initWithNormal:(NSString *)aNormal alias:(NSString *)alias;
#end
DeclaringProperties.m
#import "DeclaringProperties.h"
// private interface
#interface DeclaringProperties ()
#property (nonatomic, readwrite, retain) NSString *readonly; // readwrite for self
#property (nonatomic, readwrite, retain) NSString *private;
#property (nonatomic, readwrite, retain) NSString *retain;
#end
#pragma mark -
#implementation DeclaringProperties
#synthesize normal, alias = _alias, readonly, private, retain;
// You can not use "normal" here;
// But you can still use "alias", and it is highlighted in XCode!
- (id) initWithNormal:(NSString *)aNormal alias:(NSString *)alias {
self = [super init];
if (self) {
self.normal = aNormal;
self.alias = alias;
self.readonly = #"readonly";
self.private = #"private";
// allocated(copied) variable for retained(copied) property should be released or autoreleased
NSString *alloc = [[NSString alloc] init];
self.retain = alloc;
[alloc release];
// or
self.retain = [[NSString alloc] init];
[self.retain release];
// or
self.retain = [[[NSString alloc] init] autorelease];
// I don't like ;)
retain = [[NSString alloc] init];
}
return self;
}
- (void) dealloc {
self.normal = nil;
self.alias = nil;
self.readonly = nil;
self.private = nil;
self.retain = nil;
[super dealloc];
}
#end

Related

Why definition not required and synthesize required iphone

I have an interface
#interface A
{
NSMutableArray *_myArray;
}
#property(nonatomic, retain)NSMutableArray *myArray;
In in the implementation I have written
#synthesize myArray = _myArray;
And in the body of the code where I am using this array is only storing some arrays values which is like this...
-(void)updateArray:(NSArray*)p_NewValues
{
self.myArray = nil;
myArray = [NSMutableArray alloc]initwithArray:p_NewArray];
}
but unfortunate in code review I found that I don't required any variable definition only Synthesize is OK, can anybody explain why?
From documentation
The #synthesize directive also synthesizes an appropriate instance
variable if it is not otherwise declared.
With the modern Objective-C compiler used in the latest versions of Xcode, you don't need an explicit ivar nor do you even need the #synthesize anymore. Your code can now be:
#interface A
#property (nonatomic, retain) NSMutableArray *myArray;
#end
#implementation A
- (void)updateArray:(NSArray *)p_NewValues {
self.myArray = [NSMutableArray arrayWithArray:p_NewArray];
}
#end

No known instance method for selector error in NSMutableArray

I have a problem accessing a object in my array. I store "Place" objects in my NSMutableArray. I want to access this array for my TableView. I get the "No known instance method for selector" error in line one. See lines below.
cell.imageView = [[self.currentPlaces objectAtIndex:indexPath.row]picture];
cell.subtitleLB.text = [[self.currentPlaces objectAtIndex:indexPath.row]description];
cell.objectNameLB.text = [[self.currentPlaces objectAtIndex:indexPath.row]name];
This is my Place object:
#interface Place : NSObject{
CLLocation *objectLocation;
UIImageView *picture;
NSString *name;
NSString *description;
}
The access of the properties "description" and "name" is no problem. I just dont know why this error occurs.
Thx. Dominik
I had the same problem; what worked for me was passing the UIImage instead of the UIImageView. So your code should look like this:
#interface Place : NSObject{
CLLocation *objectLocation;
UIImage *picture;
NSString *name;
NSString *description;
}
and this
cell.imageView.image = [[self.currentPlaces objectAtIndex:indexPath.row]picture];
cell.subtitleLB.text = [[self.currentPlaces objectAtIndex:indexPath.row]description];
cell.objectNameLB.text = [[self.currentPlaces objectAtIndex:indexPath.row]name];
If that doesn't work I'll post some more code for you to look at.
You haven't actually declared any methods. What you have declared are instance variables. You should probably be using #propertys instead.
#interface Place : NSObject
#property (nonatomic, retain) CLLocation *objectLocation;
#property (nonatomic, retain) UIImageView *picture;
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy, getter=objectDescription) NSString *description;
#end
This will actually create the methods that you want. Note that I changed the method for the description property to read -objectDescription. This is because NSObject already declares the -description method and you shouldn't be overriding it with an unrelated property.
If you're on recent Clang, then this is all you need, and instance variables will get synthesized automatically (using an underbar prefix, e.g. _picture). If you're on an older version (e.g. if this causes errors), you need to add #synthesize lines, as in
#implementation Place
#synthesize objectLocation=_objectLocation;
#synthesize picture=_picture;
#synthesize name=_name;
#synthesize description=_description;
#end

Access modifier visibility in objective c

There are 3 modifiers: #private, #protected (default) and #public. So if i define a instance variable as private then that should not be accessible from anywhere.
For E.g. -
#interface A {
#private
NSString *a;
}
#property(nonatomic, retain) NSString *a;
Now inside implementation of some other interface/class B-
-(void)getSomeValue {
A *object = [[A alloc] init];
NSString *value = object.a;
.........
}
Here i am able to access instance variable, although i defined that as private.
It is a bit confusing, although when i look into details of this statement, then it is clear that it is calling the getter of a, but then also it seems confusing and it is against the concept of OOPS.
Anyone having any thought on this?
It's not the instance variable you're accessing but the property you declared. Don't declare the property if you do not want the instance variable to be visible outside the class.
#import <Foundation/Foundation.h>
#interface Visibility : NSObject {
#public
BOOL boolPublic;
#protected
BOOL boolProtected;
#private
BOOL boolPrivate;
}
#property (nonatomic, assign) BOOL boolPublic;
#property (nonatomic, assign) BOOL boolProtected;
#property (nonatomic, assign) BOOL boolPrivate;
#end
#implementation Visibility
#synthesize boolPublic;
#synthesize boolProtected;
#synthesize boolPrivate;
#end
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Visibility *visibility = [[Visibility alloc] init];
visibility.boolPublic = YES;
visibility.boolProtected = YES;
visibility.boolPrivate = YES;
// Place following NSLog()'s here
[pool release];
}
Let's try this out
Using the methods you define with #property/#synthesize
NSLog(#"Accessors %d %d %d", visibility.boolPublic, visibility.boolProtected, visibility.boolPrivate);
=> 2012-01-08 17:46:40.226 Untitled[2592:707] Accessors 1 1 1
Accessing #public ivar directly
NSLog(#"Public %d", visibility->boolPublic);
=> 2012-01-08 17:46:40.228 Untitled[2592:707] Public 1
Accessing #protected ivar directly
NSLog(#"Protected %d", visibility->boolProtected);
=> error: instance variable 'boolProtected' is protected
=> NSLog(#"Protected %d", visibility->boolProtected);
=> ^
Accessing #private ivar directly
NSLog(#"Private %d", visibility->boolPrivate);
=> error: instance variable 'boolPrivate' is private
=> NSLog(#"Private %d", visibility->boolPrivate);
=> ^
When you are accessing using dot notation this:
visibility.boolPublic
is equivalent to:
[visibility boolPublic]; // <- This is a method call
Because you set it as a #property and you claim it in header file. The variable you set as a #property will auto generate getter and setter for this variable and they are both public method to get or set it(variable is still private). If you really want to make the property as an private method, you should claim it in .m file and it will become private. You can only use this variable in the .m file.
For example, in your .h file
#interface ClassWithPrivateProperty : NSObject {
#private
NSString* member;
}
- (void) trySettingPrivateProperty;
#end
in your .m file
#import "ClassWithPrivateProperty.h"
#interface ClassWithPrivateProperty ()
#property (nonatomic,retain) NSString* member;
#end
#implementation ClassWithPrivateProperty
#synthesize member;
- (void) trySettingPrivateProperty {
self.member = #"A Value";
NSLog(#"myClass.member = %#", self.member);
}
#end
You can check more detail in Private properties for iPhone Objective-C
Edit:
Thanks for Abizern and Paul's comment, but in fact I got nothing compile error for this program.
I think RIP's question is "Why I set the variable in #private but I can still modify the variable like instance.variable"
The answer is although he set the variable as #private, but claim #property for variable in .h file also provide public methods getter and setter. So he can still get the instance variable use instance.variable. For OOP design pattern you should not expose your internals publicly. So if you want to use a variable privately only in its class and no one know it. And you still want to use getter and setter to access this variable in its class. you should claim #property in .m file like I did above. I claim the #property in .m file, it's a #interface extension(unnamed category). So you can make it "like" private. Because you cannot access this variable from anywhere outside this class. So it's just like a "private #property" that I mention about.
Two useful articles for you Public Properties with Private Setters and Private properties for iPhone Objective-C

Override #synthesize method?

I have one class called dog and another class called cat.
Dog has an variable, "name" declared with #property in the .h file.
In the cat class, I have set the name by using the command, myDog.name = "buster", after creating the variable "myDog" of type dog.
I would like to do additional operations when the name is set by overriding the set method normally created by the #synthesize command.
How can I do that? Thanks for the help!
All you have to do is leave the #synthesize then create whichever methods you want to be custom. Example:
In .h
#property(nonatomic, retain)NSString *Bob;
In .m
#synthesize bob;
-(void)setBob:(NSString *)bobValue{
[bobValue retain];
[bob release];
bob = bobValue;
//your custom stuffs here
}
This has been pretty much answered on SO already - see Objective-C synthesize property name overriding for details. In particular, #Dev Kanchen's answer which includes example code.
You cannot override (and call it within) a synthesized method from within the very same class.
You can however override it from a subclass (or rather: synthesize it in an abstract superclass).
If you simply want to perform additional (vs. different) operations upon property change I would use KVO by simply adding each dog as observer to its own "name" property in -(id)init;.
Edit:
There is a way to add additional logic to synthesized methods from within the same class:
Define a private intermediate property in a class extension.
I've attached source code for a class which uses synthesized properties and takes care(sic!) of keeping the dog's owner in sync with its own identity.
Dog.h:
#import <Foundation/Foundation.h>
#interface Dog : NSObject {
#private
NSString *name;
NSString *owner;
}
#property (nonatomic, readwrite, retain) NSString *name;
#property (nonatomic, readwrite, retain) NSString *owner;
#end
Dog.m:
#import "Dog.h"
#interface Dog ()
#property (nonatomic, readwrite, retain) NSString *primitiveName;
#end
#implementation Dog
#dynamic name;
#synthesize primitiveName = name;
#synthesize owner;
- (id)init {
if ((self = [super init])) {
name = #"Snowy";
owner = #"Tintin";
}
return self;
}
- (void)dealloc {
[super dealloc];
}
- (NSString *)name {
return self.primitiveName;
}
- (void)setName:(NSString *)aName {
self.primitiveName = aName;
if ([aName isEqualToString:#"Snoopy"]) {
self.owner = #"Charlie Brown";
}
else if ([aName isEqualToString:#"Snowy"]) {
self.owner = #"Tintin";
}
}
- (NSString *)description {
return [NSString stringWithFormat:#"<%# name:'%#' owner:'%#'>", [self class], self.name, self.owner];
}
#end
Test:
Dog *dog = [[Dog alloc] init];
NSLog(#"%#", dog);
dog.name = #"Snoopy";
NSLog(#"%#", dog);
dog.name = #"Snowy";
NSLog(#"%#", dog);
Result:
<Dog name:'Snowy' owner:'Tintin'>
<Dog name:'Snoopy' owner:'Charlie Brown'>
<Dog name:'Snowy' owner:'Tintin'>

How can I simply change a class variable from another class in ObjectiveC?

I simply want to change a variable of an object from another class. I can compile without a problem, but my variable always is set to 'null'.
I used the following code:
Object.h:
#interface Object : NSObject {
//...
NSString *color;
//...
}
#property(nonatomic, retain) NSString* color;
+ (id)Object;
- (void)setColor:(NSString*)col;
- (NSString*)getColor;
#end
Object.m:
+(id)Object{
return [[[Object alloc] init] autorelease];
}
- (void)setColor:(NSString*)col {
self.color = col;
}
- (NSString*)getColor {
return self.color;
}
MyViewController.h
#import "Object.h"
#interface ClassesTestViewController : UIViewController {
Object *myObject;
UILabel *label1;
}
#property UILabel *label1;
#property (assign) Object *myObject;
#end
MyViewController.m:
#import "Object.h"
#implementation MyViewController
#synthesize myObject;
- (void)viewDidLoad {
[myObject setColor:#"red"];
NSLog(#"Color = %#", [myObject getColor]);
[super viewDidLoad];
}
The NSLog message is always Color = (null)
I tried many different ways to solve this problem, but no success.
Any help would be appreciated.
Thanks for the help so far.
I modified the code as follow, but it still doesn't work as it should.
MyViewController.h:
#import <UIKit/UIKit.h>
#import "Object.h"
#interface MyViewController : UIViewController {
Object *myObject;
}
#property (nonatomic, retain) Object *myObject;
#end
MyViewController.m:
#import "MyViewController.h"
#import "Object.h"
#implementation MyViewController
#synthesize myObject;
- (void)viewDidLoad {
Object *myObject = [Object new];
myObject = 0;
[myObject setColor:#"red"];
NSLog(#"color = %#", myObject.color);
[super viewDidLoad];
}
If I do it like this, NSLog returns color = null (and I think myObject is only visible in viewDidLoad). How can declare myObject and make it visible in MyViewController?
I stripped down my Object class to
Object.h:
#interface Object : NSObject {
NSString *color;
}
#property(nonatomic, retain) NSString *color;
#end
Object.m:
#import "Object.h"
#implementation Object
#synthesize color;
#end
I wasn't able to define an object myObject in ViewDidLoad so that I can access its properties from the whole ViewController class? What did I miss?
Side question: Why do I have to set myObject to 0?
You're declaring a property, then explicitly declaring the accessors in Object.h. You only need to do one or the other - they mean the same thing (well, almost - you'll have color instead of getColor)
To implement the property in Object.m you should use #synthesize color. The explicit implementations, again, are then redundant (unless they do anything extra).
The explicit setColor implementation in Object.m is calling the property - which you are implementing explicitly, so I would have expected you to get an infinite recursion here.
MyViewController.m should probably synthesize label1, since you declare the property in the header (although it's not being used in your snippet).
[myObject getColor] is calling the color property, which you declared but did not synthesize. If you had explicitly implemented it as color it would have picked that up - but it won't match getColor (which is fortunately as that would have led to an infinite recursion again.
I don't see anywhere where you create your myObject instance. If you don't it will be nil and methods called on it (including property accesses) will return 0 or nil.
I suspect (6) is the cause of your issue, but the others need to be addressed too. Make sure you read up on property syntax.