What is the point of #property and #synthesize? - iphone

I haven't been able to figure it out, and there are no websites which explain it clearly enough... what exactly are the purposes of #property and #synthesize?
Thanks in advance!

Objective-C Runtime Programming Guide: Declared Properties
#property declares the getter and the setter methods for the public property you want to implement. For example this property declaration:
#property float value;
is equivalent to:
- (float)value;
- (void)setValue:(float)newValue;
#synthesize provides default implementation for these two accessors.
Update: The above explains what these two do. It does not explain what their purpose is. :-)
#property adds a member to the public interface that acts as a data variable to your class clients, but is read and written using methods. This gives you better control over the data that is exchanged between the client and your code, for example you can do extended validation on the values your code is given.
#synthesize allows you to not explicitly write the code that will be called by the client and actually treat the property as a data variable yourself.

The "#" symbol is interpreted by the compiler as a directive. This is one of the Objective-C 'additions' to the C language. When you declare #property and then #synthesize you are instructing the compiler to create the instructions and corresponding symbols for getters and setters for you. Remember that in the C language, the "=" operator means "assign". Most of the time in the OO context that the Objective-C extensions provide, we are working with pointers (aka references) to isa data structures (Classes in Objective-C).
Prior to Objective-C 2.0, all of the getter and setter methods had to be coded by the developer for every attribute which for most cases was copy/paste code. To be completely KVC/KVO compliant requires a lot of very tedious code... willAccessValueForKey, didUpdateValueForKey statements etc. that the new compiler adds for you automatically when you use the #property/#synthesize syntax. This is a huge productivity boost for developers. The dot syntax additions to the language are a little more contentious in the community as this hides the magic the compiler is doing on you behalf to interpret the object.property = anotherObject.property; statement as [object setProperty:[anotherObject property]];
From the Apple documentation referenced in other answers
Property Declaration Attributes
You can decorate a property with attributes by using the form #property(attribute [, attribute2, ...]). Like methods, properties are scoped to their enclosing interface declaration. For property declarations that use a comma delimited list of variable names, the property attributes apply to all of the named properties.
If you use the #synthesize directive to tell the compiler to create the accessor method(s), the code it generates matches the specification given by the keywords. If you implement the accessor method(s) yourself, you should ensure that it matches the specification (for example, if you specify copy you must make sure that you do copy the input value in the setter method).

I hope this will help you.
#property and #synthesize is Use to Access Object Or Variable into Another Class.
Here is a Small Example:
This is First Class
#import <UIKit/UIKit.h>
#import "ClassB.h"
#interface ViewController : UIViewController
#property(nonatomic, retain) NSString *FirstName;
#property(nonatomic, retain) NSString *LastName;
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize FirstName, LastName;
- (void)viewDidLoad
{
[super viewDidLoad];
self.FirstName = #"Ashvin";
self.LastName = #"Ajadiya";
ClassB *ClassBOb = [[ClassB alloc] init];
ClassBOb.ViewCntrlrOb = self;
[ClassBOb CallMe];
}
#end
And This is Another Class:
#import <UIKit/UIKit.h>
#class ViewController;
#interface ClassB : UIViewController
#property(nonatomic, retain) ViewController *ViewCntrlrOb;
-(void) CallMe;
#end
#import "ClassB.h"
#import "ViewController.h"
#interface ClassB ()
#end
#implementation ClassB
#synthesize ViewCntrlrOb;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void) CallMe
{
NSLog(#"FirstName = %#",ViewCntrlrOb.FirstName);
NSLog(#"LastName = %#",ViewCntrlrOb.LastName);
}
So You can Access FirstName And LastName into ClassB.
And They Print:
2012-05-25 14:38:10.766 MyExample[8751:c07] FirstName = Ashvin
2012-05-25 14:38:10.768 MyExample[8751:c07] LastName = Ajadiya

Just a quick example of why you might not want to do just "variable = 0":
Say you have this property:
#property (nonatomic, retain) id <MyDelegate> theDelegate;
Whenever you replace that delegate with a new one, your synthesized setters and getters will handle the release/retain for you every time you set it like so:
self.theDelegate = newObject;
Really what happened was this:
[self setTheDelegate:newObject];
- (void)setTheDelegate:(id <MyDelegate>)anObject {
[theDelegate release];
theDelegate = [anObject retain];
}
(This is simplified of course)
You can do very powerful things in your own setters and getters, synthesize is for those that happen over and over like retained properties, etc. When compiling it looks at your #property and builds the methods accordingly.

Related

Instance variable with property & synthesize and one without them - ARC

You can declare a variable like this.
Case1:
#interface MyClass : NSObject
{
NSString *str;
}
#end
Also, if you want to set its property, you can do
Case2:
#interface MyClass : NSObject
{
NSString *str;
}
#property (nonatomic, strong) NSString *str;
#end
And in the .m,
#synthesize str;
My understanding with the difference between Case 1 and Case 2 is that synthesized and propertied variables in Case 2 can be accessed from another class when this another class instantiates this class.
What are other differences between Case 1 and 2? Say when these variables are just used only in its .m file. The fact that you are setting 'str' properties probably makes a difference, but how? If you don't set property, how are they going to be released with ARC?
The differences are fairly confusing in this case because of the way it is set up.
Also it is using what is now old practises.
The new suggested way of doing this (suggested by Apple) is to do this...
MyClass.h
#interface MyClass : NSObject
#property (nonatomic, strong) NSString *str;
#end
MyClass.m
#import "MyClass.h"
#implementation MyClass
#end
You no longer need the #synthesize as Xcode (since 4.5) will auto generate these for you.
Doing this sets up the property called str and an iVar called _str.
You now no longer need to worry about defining multiple ivars and properties etc... Just use the property and that's it done.
An example setter method for the property str would look like this...
- (void)setStr:(NSString*)str
{
_str = str;
}

Objective c: i want the automatic memory management of properties, but no access from other classes

I've kind of been confused about properties. Some people say to always use setters and getters for ivars, even within the ivar's class. So if "name" is an ivar, when referring to it, one should always use "self.name". Always. Even if you're in the same class that "name" is declared in.
First, is that correct advice?
Second, what if I wish to reap the automatic memory management that comes with declaring "name" as a property and synthesizing it, but I don't want to give other classes access to change "name"? I guess it would be sort of a private property?
Thanks!
Yes, you should always try to use the property accessors when possible. Using ARC alleviates these concerns somewhat, but it's still good style. As for your second question, you can declare the property as readonly in the public header file and redefine it in a class extension:
In MyClass.h:
#interface MyClass : NSObject
#property (strong, readonly, nonatomic) id foo;
#end
In MyClass.m:
#interface MyClass()
#property (strong, readwrite, nonatomic) id foo;
#end
#implementation MyClass
#synthesize foo = _foo;
// The rest of your code goes here.
#end
This will allow you to call [self setFoo:foo] all day inside of MyClass’s implementation, but not other classes.
For ivars which are accessed externally, I generally use properties to access the ivar from within the class, for ivars which are only used internally (usually BOOL, NSUInteger, NSInteger, etc), I use the ivar directly. I do however access an consistently within the class (i.e. if I'm using a property to access it, I always use a property).
For the second part of your question. You can create a readonly property in the class's interface definition and within the same file as the implementation create a category with the read-write property. For example:
MyClass.h
#interface MyClass : NSObject
{
NSString * name;
}
#property (nonatomic, readonly) NSString * name;
#end
MyClass.m
#interface MyClass()
#property (nonatomic, retain) NSString * name;
#end
#implementation MyClass
#synthesize name;
-(void)dealloc
{
[name release];
[super dealloc];
return;
}
#end
Keep in mind, that although another class accessing the method -setName: may cause compile warnings or errors, another class may still call -(id)performSelector:withObject: with without an error.
For instance:
MyClass * test = [[MyClass alloc] init];
test.name = #"David";
is functionally the same as:
MyClass * test = [[MyClass alloc] init];
[test performSelector:#selector(setName:) withObject:#"David"];

ViewController = _ViewController meaning [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Prefixing property names with an underscore in Objective C
I just started iphone App development and noticed that when you generate a new project, the following code can be seen in AppDelegate.m
#synthesize window = _window;
#synthesize viewController = _viewController;
AND in the AppDelegate.h file it says
#property (strong, nonatomic) UIWindow window;
#property (strong, nonatomic) ViewController controller;
I would just like to know what exactly this means, particularly the synthesizing part. Is it
instantiating a local private variable? If so, how is this different from saying
#synthesize viewController;
Thanks
The pattern #synthesize foo = bar; allows you to define a property of key foo which gets synthesized in combination with an instance variable of name bar (or _foo if you want), while #synthesize foo; simply synthesizes a property and instance variable with the same name (foo that is).
#property (...) Foo *foo;
#synthesize foo = _foo;
is kind of equivalent to this:
#interface MyClass : NSObject {
//result of #synthesize...:
Foo *_foo;
}
//result of #property...:
- (void)setFoo:(Foo *)foo;
//result of #property...:
- (Foo *)foo;
#end
#implementation MyClass
//result of #synthesize...:
- (void)setFoo:(Foo *)foo {
_foo = foo; //simplified!
}
//result of #synthesize...:
- (Foo *)foo {
return _foo; //simplified!
}
#end
The synthezised instance variable would the be used via either _foo or self->_foo (of which the former actually is an implicit form), which would involve no accessor method call whatsoever.
While the synthezised property would the be used via self.foo, which would then utilize a call to one of the synthesized accessor methods.
Just think of #synthesize foo; as an implicit #synthesize foo = foo; (note the lack of an _ here, equal names).
More or less. These lines in the .h declare the existence of two public variables called window and controller:
#property (strong, nonatomic) UIWindow window;
#property (strong, nonatomic) ViewController controller;
But these lines only declare the existence of the variables, they don't actually create them. It's up to the class to implement these however it wants - they could be virtual variables for example, that don't actually exist but call methods that create data programmatically, or load it from a database or something.
These lines in the .m file actually create ("synthesize") the variables.
#synthesize window = _window;
#synthesize viewController = _viewController;
What these lines actually say is that the internal variable name is _window, but the public name of the variable is window. That means that within the class you can access the variable directly by saying
_window = something;
But externally you have to access it using
appDelegate.window = something;
Because that's it's public name. You can also access it internally to the class using self.window.
Another interesting fact of Objective-C is that using dot syntax to access variables in this way is really just a handy way of calling setter and getter methods to access them. SO the synthesize line, in addition to creating a variable called _window, is also defining the following two methods:
- (void)setWindow:(UIWindow *)window; // to set the _window variable
- (UIWindow *)window; // to get the _window variable
And you can call these methods directly if you like, using
[self setWindow:someValue];
UIWindow *window = [self window];

iPhone sythesizing

Whilst learning Objective C I've gotten used to how the #property and #synthesize works.
//usual way
#synthesize world;
But recently I've come across practices were #synthesize is like this:
//new way
#synthesize world=world_;
Could someone explain why this technique is used please?
Thanks,
Mark
I believe this is used to avoid confusion between the actual ivar name and the property name.
so you can have
#interface MyClass : NSObject
{
NSObject *_myObjActualIVar;
}
#property (retain) NSObject *myObjProperty;
#end
#implementation MyClass
#synthesize myObjProperty = _myObjActualIVar;
- (void) someMethod: (NSObject *)someOtherObject
{
[_myObjActualIVar release]; // here I know I'm accessing the ivar
self.myObjProperty = someOtherObject; // here I know I'm using the setter method
//myObjProperty = someOtherObject; // if I accidentally try to do this
//self._myObjActualIVar; // or this, the compiler will tell me
}
#end
and then it makes it harder for you or some other developer working on this class to inadvertently access the ivar when you actually wanted the property or vice-versa. The different names make it clearer which is which.
from the Objective-C 2 manual:
You can use the form property=ivar to
indicate that a particular instance
variable should be used for the
property, for example:
#synthesize firstName, lastName, age = yearsOld

Object as a data member in Objective C

From what I have experienced it seems as if objects cannot be shared data members in objective c. I know you can init a pointer and alloc the object in each method but I cannot seem to figure out how one can say define a NSMutableString as a data member and allow all of the methods to use and modify its data as in c++. Is this true or am I missing something?
To define an instance variable (member), edit your .h file:
#interface MyClass : NSObject {
// ivars go here
NSObject *member;
}
// methods go here
#end
Then, in your .m file, from any instance method (one which begins with -), you can access this variable.
- (void)doThingWithIvar {
[member doThing];
}
If you want to access the variable from outside the object itself, you'll need accessors. You can do this easily with Obj-C properties:
#interface MyClass : NSObject {
// ivars go here
NSObject *member;
}
// methods go here
#property (nonatomic, retain) NSObject *member;
#end
And in the .m:
#implementation MyClass
#synthesize member;
// ...
#end
The #synthesize line creates getter/setter methods for the ivar. Then you can use property syntax:
MyClass *thing = ...;
NSLog(#"%#", thing.member); // getting
thing.member = obj; // setting
(Note that I specified (retain) for the #property; if your member isn't an Objective-C object you won't want that. And if your property's class has a mutable counterpart, you'll want (copy) instead.)
It sounds like you want to synthesize (create getter/setter methods) a property for a member variable. I just found this cheat sheet, go down to the section called, "Properties", should give a quick overview.
Other than that Apple's documentation should give you more info.