#synthesize behind the scenes - objective-c

When we create a property and defined a synthesize for it, the compiler automatically creates getters and setters methods, right?
Now, If I execute this command:
#property(nonatomic) int value;
#synthesize value;
value = 50;
What happens:
The compiler saves the value '50' in the property?
property (nonatomic) int value; // Here is the stored value 50!
or the compiler creates a variable behind the scenes with the same name of the property like this:
interface myClass: NSObject {
int value; // Here is the stored value 50!
}
What actually happens and what the alternatives listed above is correct?

It's probably semantics, but #synthesize is no longer required for properties. The compiler does it automatically.
But to answer your question, the compiler creates an instance variable to store the value for the property.
#property (nonatomic, assign) NSInteger value;
// an instance variable NSInteger _value; will be created in the interface.
You can use the instance variable in your own code if you need to access it without going through the property. This is common when overriding a setter, like so:
- (void)setValue:(NSInteger)value {
_value = value;
// custom code
}

Related

Objective-C: Overriding Getter & Setter with Instance Variable (using _) [duplicate]

This question already has answers here:
Error accessing generated ivars when I override setters and getters in Modern Objective-C
(3 answers)
Closed 5 years ago.
I'm learning the Swift programing language and during this I sometimes get in touch with the old Objective-C programming language and its code.
I'm an absolutely beginner and therefore I have some question for better understanding the Setter and Getter.
So, I know that I can create an instance variable through curly braces in the .h file but normally I use properties. These properties are backed by an instance variable and offer automatically a Getter and Setter Method.
Example:
Vehicle .h file:
#interface Vehicle : NSObject
#property int myProperty;
#end
Because I created this property I don't have to declare a Getter and Setter method in the vehicle.m file because they are automatically created by the compiler. So I can create a vehicle-object, set and get the value.
Example
main.m
Vehicle *myvehicle = [[vehicle alloc] init];
[myvehicle myProperty] // myvehicle.myProperty
[myvehicle setMyProperty : 10] // myvehicle.myProperty = 10;
Now I read that it is possible to override the automatically created Getter and Setter method of my created property "myProperty". When declaring my own version of the Getter and Setter I have to declare two methods in the vehicle.h and vehicle.m file. In the vehicle.m file I don't call the object by using the self keyword but by using it's automatically created instance variable (_myProperty). Is it right?
I tried it but alway get an error and I don't know why and what is the point.
Example
Vehicle .h file:
#interface Vehicle : NSObject
#property int myProperty;
-(int) myProperty; //my new Getter method
-(void) setMyProperty: (int)updatedMyProperty; //My new Setter method
#end
vehicle .m file:
#implementation Vehicle
-(int) myProperty {
if (! _myProperty) {
_myProperty = NO;
}
return _myProperty;
}
-(void) setMyProperty: (int)updatedMyProperty {
if (_myProperty == updatedMyProperty) return;
_myProperty = updatedMyProperty;
}
#end
I always get the error "Use of undeclared identifier" and I don't know why. If I understand right I don't have to declare the ivar or its name using #synthesize because the compiler automatically creates the ivar called _myProperty for me. I just have to use #synthesize when I want to change the ivar's name.
I'm not sure why I get stuck and what the point is. Could you explain it?
Thanks in advance!
If you implement all of the accessor methods, the compiler will no longer automatically synthesize the ivar for you. In this case, you have to explicitly do so yourself. E.g.
#synthesize myProperty = _myProperty;
This is only necessary when you have manually implemented all of the accessor methods. The reason is that the compiler is smart enough to know that if you're taking over the accessor methods, you may well not need the ivar, namely that you might be doing something radically different, e.g. computing values from some other property, setting/getting values from some different store, etc. You may want the compiler to synthesize the ivar (in which case you add the above #synthesize statement), but it's equally likely that you've implemented the accessor methods because no backing ivar is needed (in which case you'd omit the above #synthesize statement).
Anyway, staying with your simple example, you get something like:
#interface Vehicle : NSObject
#property (nonatomic) int myProperty; // if you don't write atomic accessor methods, you really should be explicit that this is nonatomic
// as an aside, even if you implement accessor methods, you don't have to declare them here
//
// -(int) myProperty; //my new Getter method
// -(void) setMyProperty: (int)updatedMyProperty; //My new Setter method
#end
And
#implementation Vehicle
// since you implemented all of the accessor properties, you have to manually synthesize the ivar
#synthesize myProperty = _myProperty;
- (int) myProperty {
// do whatever you want here; note, the following doesn't make sense
//
// if (! _myProperty) {
// _myProperty = NO;
// }
return _myProperty;
}
- (void)setMyProperty:(int)updatedMyProperty {
if (_myProperty == updatedMyProperty) return;
_myProperty = updatedMyProperty;
}
#end
Clearly, there's no point in writing these particular accessor methods in the above example, because you're not offering any new functionality, so you wouldn't. You'd just avail yourself of the auto-synthesized accessor methods.
But in those cases that you really need to write your own accessor methods, then you have to explicitly tell the compiler whether you need it to synthesize the ivar for you, too, or not.

Is it possible to decoupling Setters/Getters from data in Objective-C Properties?

I understand that Properties in Objective-C allows us to compactly define:
setter
getter
data
I would like to use Properties, but it would be nice if I could decouple the data from the getter/setter.
In other words, I like the getter/setter interface of Properties, but I would like to define my own internal representation of data.
For instance, if I were to define MyOwnTime class, I want getters/setters for Properties like hour, date, and minute (as a consumer, I want to be able to set them and get them). However, to save memory in the representation, rather than store date, month, year, hour, minute, second, etc..., I prefer to store secondsSinceEpoch as an unsigned long.
In my situation should I/can I use Properties? How would I do this? Or should I manually roll my own setters and getters?
You can certainly do this. Indeed, it's pretty common. Since the mapping to data is not direct, you cannot synthesise the methods, you have to write the getters and setters manually. But the property will work like any other.
As a simple dummy example:
#interface BytePair
{
UInt16 data;
}
#property UInt8 loByte;
#property UInt8 hiByte;
#end
#implementation BytePair
- (UInt8) loByte
{
return (UInt8) data & 0xff;
}
- (void) setLoByte:(UInt8)lo
{
data = (data & 0xff00) | lo;
}
- (UInt8) hiByte
{
return (UInt8) (data & 0xff00) >> 8;
}
- (void) setHiByte:(UInt8)lo
{
data = (data & 0xff) | (lo << 8);
}
#end
Or whatever. You get the idea.
Of course you can :
#interface MyObject : NSObject
{
int toto;
}
#property(nonatomic, setter=mySetterMethod:, getter=myGetterMethod) int toto;
-(void) mySetterMethod:(int) t;
-(void) myGetterMethod;
Or you can also override the setter and getter default methods, in my case (in the .m file) :
-(int) toto
{
return toto;
}
-(void) setToto:(int) t
{
toto = t;
}
Declare a
#property (nonatomic, assign) sometype somename
in the interface as normal. Then instead of writing
#sythesize somename = _somename
in the implementation, you write
-(sometype)somename {
return whatever;
}
-(void)setSomename(sometype)newValue {
whatever;
}
You don't need to use the #property notation at all. Just do this:
#interface MyObject
- (id)foo;
- (void)setFoo:(id)newFoo;
#end
Then, elsewhere you can do myObject.foo = #"bar";.
Calling myObject.foo is exactly the same as [myObject foo] or [myObject setFoo:foo]. It has nothing to do with properties, it just happens to be most commonly used for them.
And the #property syntax is just a formal way of declaring properties allowing you to do more advanced stuff (like nonatomic). If you're defining your own methods, instead of letting the compiler define them for you, then there isn't much point to using #property.
If you have ARC disabled, then you might want to look into how to properly memory manage a property, as there are some non-obvious edge cases where you can get into trouble defining your own data storage code.
You can, it's common, and pretty easy to do.
As with a standard property, you don't even need to declare an instance variable for that and can still #synthesize your property (contrary to what other answers are saying), you simply have to override the setter of the property:
#interface MyObject : NSObject
#property(nonatomic, copy) NSString* myprop; // like any other property
#property(nonatomic, readonly) BOOL hasProp;
#end
#implementation MyObject
#synthesize myprop = _myprop; // optional with latest LLVM compiler, will generate the _myprop instance variable at compile time
// Override default setter for myprop
-(void)setMyprop:(NSString*)newvalue
{
if (_myprop != newvalue)
{
[_myprop release]; // release only necessary if not using ARC
_myprop = [newvalue retain]; // retain only necessary if not using ARC
// And/Or whatever you want your custom setter to do
}
}
// You can override default "-(NSString*)myprop" getter too if you want
// You you can keep the default getter implementation and only override the setter.
// Or vice-versa. It's really up to you
// Another example: we created a #property(readonly) hasProp and implement its getter by ourselves, without any dedicated instance variable
-(BOOL)hasProp
{
return (self.myprop != nil);
}
#end

Add a property without instance variable?

I'm trying to add a property without creating an instance variable. Is it possible to do this? Or can you do something similar in a way that's not a property?
Example:
#interface RandomClass()
#property (nonatomic) int value;
#end
#implementation RandomClass
#synthesize value = _value;
// Here I override the default methods #synthesize
-(int)value
{
return 8; // Actually I'm returning something more complex, so a "define" won't work
}
-(void)setValue:(int)value
{
self.someOtherValue = value;
}
In the code above, I'm not using the instance variable _value! Is there a way to do this without creating the variable?
Remove the line
#synthesize value = _value;
Since you're implementing the getter/setter yourself, the #synthesize isn't helpful.
#synthesize serves two jobs. The first job is to connect the property to a backing ivar, synthesizing the ivar if it doesn't already exist. The second job is to synthesize the getter/setter. If you don't need the backing ivar, and if you're providing implementations for the getter/setter yourself, then you don't need the #synthesize at all.

#property and setters and getters

If I create a #property and synthesize it, and create a getter and setter as well like so:
#import <UIKit/UIKit.h>
{
NSString * property;
}
#property NSString * property;
--------------------------------
#implementation
#synthesize property = _property
-(void)setProperty(NSString *) property
{
_property = property;
}
-(NSString *)property
{
return _property = #"something";
}
Am I correct in assuming that this call
-(NSString *)returnValue
{
return self.property; // I know that this automatically calls the built in getter function that comes with synthesizing a property, but am I correct in assuming that I have overridden the getter with my getter? Or must I explicitly call my self-defined getter?
}
is the same as this call?
-(NSString *)returnValue
{
return property; // does this call the getter function or the instance variable?
}
is the same as this call?
-(NSString *)returnValue
{
return _property; // is this the same as the first example above?
}
There are a number of problems with your code, not least of which is that you've inadvertently defined two different instance variables: property and _property.
Objective-C property syntax is merely shorthand for plain old methods and instance variables. You should start by implementing your example without properties: just use regular instance variables and methods:
#interface MyClass {
NSString* _myProperty;
}
- (NSString*)myProperty;
- (void)setMyProperty:(NSString*)value;
- (NSString*)someOtherMethod;
#end
#implementation MyClass
- (NSString*)myProperty {
return [_myProperty stringByAppendingString:#" Tricky."];
}
- (void)setMyProperty:(NSString*)value {
_myProperty = value; // Assuming ARC is enabled.
}
- (NSString*)someOtherMethod {
return [self myProperty];
}
#end
To convert this code to use properties, you merely replace the myProperty method declarations with a property declaration.
#interface MyClass {
NSString* _myProperty;
}
#property (nonatomic, retain) NSString* myProperty
- (NSString*)someOtherMethod;
#end
...
The implementation remains the same, and works the same.
You have the option of synthesizing your property in your implementation, and this allows you to remove the _myProperty instance variable declaration, and the generic property setter:
#interface MyClass
#property (nonatomic, retain) NSString* myProperty;
- (NSString*)someOtherMethod;
#end
#implementation MyClass
#synthesize myProperty = _myProperty; // setter and ivar are created automatically
- (NSString*)myProperty {
return [_myProperty stringByAppendingString:#" Tricky."];
}
- (NSString*)someOtherMethod {
return [self myProperty];
}
Each of these examples are identical in how they operate, the property syntax merely shorthand that allows you to write less actual code.
return self.property – will call your overridden getter.
return _property; – accesses the property's instance variable directly, no call to the getter.
return property; – instance variable.
EDIT: I should emphasize that you will have two different NSString variables -- property and _property. I'm assuming you're testing the boundaries here and not providing actual production code.
above answer elaborate almost all the thing , i want to elaborate it little more.
// older way
#interface MyClass {
NSString* _myProperty; // instance variable
}
- (NSString*)myProperty; // getter method
- (void)setMyProperty:(NSString*)value;//setter method
#end
the instance variable can not be seen outside this class , for that we have to make getter and setter for it.
and latter on synthesis it in .m file
but now
we only used
#property(nonatomic) NSString *myProperty;
the #property is an Objective-C directive which declares the property
-> The "`nonatomic`" in the parenthesis specifies that the property is non-atomic in nature.
-> and then we define the type and name of our property.
-> prototyping of getter and setter method
now go to .m file
previously we have synthesis this property by using #synthesis , now it also not required it automatically done by IDE.
little addition : this `#synthesis` now generate the getter and setter(if not readonly) methods.

Difference between #interface declaration and #property declaration

I'm new to C, new to objective C. For an iPhone subclass, Im declaring variables I want to be visible to all methods in a class into the #interface class definition eg
#interface myclass : UIImageView {
int aVar;
}
and then I declare it again as
#property int aVar;
And then later I
#synthesize aVar;
Can you help me understand the purpose of three steps? Am I doing something unnecessary?
Thanks.
Here, you're declaring an instance variable named aVar:
#interface myclass : UIImageView {
int aVar;
}
You can now use this variable within your class:
aVar = 42;
NSLog(#"The Answer is %i.", aVar);
However, instance variables are private in Objective-C. What if you need other classes to be able to access and/or change aVar? Since methods are public in Objective-C, the answer is to write an accessor (getter) method that returns aVar and a mutator (setter) method that sets aVar:
// In header (.h) file
- (int)aVar;
- (void)setAVar:(int)newAVar;
// In implementation (.m) file
- (int)aVar {
return aVar;
}
- (void)setAVar:(int)newAVar {
if (aVar != newAVar) {
aVar = newAVar;
}
}
Now other classes can get and set aVar via:
[myclass aVar];
[myclass setAVar:24];
Writing these accessor and mutator methods can get quite tedious, so in Objective-C 2.0, Apple simplified it for us. We can now write:
// In header (.h) file
#property (nonatomic, assign) int aVar;
// In implementation (.m) file
#synthesize aVar;
...and the accessor/mutator methods will be automatically generated for us.
To sum up:
int aVar; declares an instance variable aVar
#property (nonatomic, assign) int aVar; declares the accessor and mutator methods for aVar
#synthesize aVar; implements the accessor and mutator methods for aVar
This declares an instance variable in your object:
#interface myclass : UIImageView {
int aVar;
}
Instance variables are private implementation details of your class.
If you want other objects to be able to read or set the value of the instance variable (ivar), you can declare it as a property:
#property int aVar;
This means that the compiler expects to see setter and getter accessor methods for the property.
When you use the #synthesize keyword, you are asking the compiler to automatically generate setter and getter accessor methods for you.
So, in this case the compiler will generate code similar to this when it encounters the #synthesize keyword:
- (int) aVar
{
return aVar;
}
- (void)setAVar:(int)someInt
{
aVar = someInt;
}
By default on the iPhone (and on the 32-bit runtime on the Mac), #synthesize requires an instance variable to be present in order to store the property's value. This ivar is usually named the same as the property, but doesn't have to be, for instance you could do this:
#interface myclass : UIImageView {
int aVar;
}
#property int someValue;
#synthesize someValue = aVar;
Neither #synthesize nor #property are actually required, you can create your own getter and setter methods, and as long as you create them using Key-Value Coding-compliant syntax, the property will still be usable.
The requirement for an ivar to be present as well as the #property declaration is due to the fragile base class limitation of the 32-bit Objective-C runtime on both the Mac and iPhone. With the 64-bit runtime on the Mac you don't need an ivar, #synthesize generates one for you.
Note that there are numerous keywords you can use with your #property declaration to control what sort of synthesized accessor code is created, such as readonly for a getter-only accessor, copy, atomic, nonatomic and so on. More information is in the Objective-C 2.0 Programming Language documentation.
Classes can have instance variables (ivars). These are in the first section, and are only visible to code in that class, not any outside code. I like to prefix them with an underscore to show their internal-ness. In low level terms, the ivars are added as an additional member to the struct that the class you are creating uses internally.
The second declaration, #property, is a declared property. It is not required (except when you are using #synthesize), but it helps other programmers (and the compiler!) know that you are dealing with a property, and not just two methods -setAVar and -aVar, which is the alternative way of doing this.
Thirdly, the #synthesize actually creates the methods to set and access the property from outside the class. You can replace this with your own setter and getter methods, but only do that if you need to, as the built-in ones have some features that you would otherwise have to code yourself. In fact, using the #synthesize aVar = _someVariable; syntax, you can have your property actually reference a differently named instance variable!
Short version:
The #property is just a hint to the compiler and other programmers that you are making a property and not just getter/setter methods. The instance variables are internal to the class, and otherwise cannot be normally accessed from outside it. The #synthesize just creates simple getters and setters for you, to go with your #property, but you can also just code those getters and setters yourself, like any other method.
Class A
#interface myclass : UIImageView {
int aVar;
}
If you declare like this then you can only use this variable within your class A.
But suppose in Class B
A *object=[A new];
object.aVar---->not available
For this you should **declare aVar as a property in Class A**
so class A should look like
Class A
#interface myclass : UIImageView {
int aVar;
}
#property int iVar;
and
.m file
#synthesize iVar;
So now you can use this iVar in another class Suppose B
Class B
#import "Class A.h"
enter code here
A *object=[A new];
object.aVar---->available
means
object.aVar=10;
#interface declares the instances variables of a class in obj-c. You need it to create an instance variable. However the variable is not visible outside the class by default (as the field is by default protected).
#property tells the compiler to specify a particular property accessor (get/set) method. However, you will need to use #synthesize to actually have the compiler generate the simple accessors automatically, otherwise you are expected to create them on your own.
I recently started learning iphone apps. As per my knowledge #property is used in .h file as a setter method and #synthesize in .m file as getter method. In Java we use setter and getter methods, same as Java, in Objective C we use #property and #synthesize.
Please forgive me If u think I mislead you.