Synthesize in Objective C [duplicate] - objective-c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
#property #synthesize
Explain the working or purpose of synthesize

Basically, #synthesize automatically generates the getters and setters based on the #property declaration.

synthesize used with property.When you declair a property in .h file then you need to synthesize that in .m file.
#synthesize creates getter and setter for your property.If you do not synthesize the property then you can use property.(it gives crash).
var=self.yourProperty (calling getter). simmiler to var=[self getYourProperty];
self.yourProperty=var (calling setter). simmiler to [self setYourProperty:var];
without synthesize you cant use the setter and getter.

Related

When to synthesize objective [duplicate]

This question already has answers here:
When should I use #synthesize explicitly?
(7 answers)
Closed 8 years ago.
I am having some difficulty understanding what objects are supposed to be synthesized. For example:
#interface DoSomething : UIView
#property (strong, nonatomic) UIColor *frameColor;
#property BOOL toggleScrollability;
- (void) changeBackgroundColorOfView;
#end
In the .m file, which of these three items should be synthesized? Is there any disadvantage if I try and synthesize them all? In general, what is the rule of thumb for what objects you are supposed to synthesize?
The first two are properties; the third is an instance method. #synthesize applies only to properties.
However, if you're building for iOS 6 or newer, you don't need to synthesize at all. The compiler has handled this automatically for the last few years now.
You can only synthesize properties, so you won't be able to write #synthesize changeBackgroundColorOfView.
Since XCode 4 I think, you don't have to use #synthesize anymore. The compiler automatically add it when it needs to be and you can access it by adding a _ before the name of your property.
In your example, you will access the frameColor property as _frameColor if you are in the DoSomething class, doSomethingInstance.frameColor if not.
But, you can always add it yourself if you want to rename your property for internal stuff.
See Apple's reference.

Data Hiding and Objective-C synthesisers [duplicate]

This question already has answers here:
How to make a real private instance variable?
(7 answers)
Closed 8 years ago.
How exactly should attributes be declared if they are needed to be private and the language supports automatic getter/setter method creation?
Is the only way to override the automatically created getter or setter as needed?
In the top of the .m (implementation) file:
// Private category on your class, declared at top of implementation file.
#interface MyClass ()
#property (nonatomic, copy) NSString * privateString;
#end
#implementation
...
#end
These "private properties" are visible only within your implementation.
Please note that ObjC has no facility for runtime access restriction. Other objects can still call your private getters and setters if they want to (although this will generate compiler warnings).

Property Definition in h.file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
iOS: must every iVar really be property?
I really would appreciate if somebody could explain to me why some properties are defined in the interface statement and some as #property ones.
#interface PlacesParser : NSObject
{
NSMutableArray *arrPlaces;
TBXML *tbxml;
}
#property(nonatomic,retain) NSMutableArray *arrPlaces;
-(void)loadRecords:(NSString *)records;
-(void)traverseElement:(TBXMLElement *)element;
#end
In my example we got "arrPlaces" as #property
and "arrPlaces" within the interface.
The thing in the interface is not actually a property, it is just a plain old instance variable. The #property statement is what makes it a property (giving you the setters and getters). You used to need both, but you don't anymore. If you want your field to be a property with those generated methods, you can leave out the initial field declaration.
What is the difference between ivars and properties in Objective-C
This thread really opened my eyes. Great explonation to my question.

Why won't my delegate accept performSelectorOnMainThread:withObject:waitUntilDone:? [duplicate]

This question already has answers here:
Compiler gives warning on performSelectorOnMainThread:#selector(delegateMethod)
(4 answers)
Closed 10 years ago.
Xcode 4 is giving me compiler warnings on the performSelectorOnMainThread:withObject:waitUntilDone: message sent to my delegate and I don't get it.
My delegate is declared like:
#property (nonatomic, assign) id <AccountFeedbackDelegate> delegate;
And then eventually executed on the main thread:
[self.delegate performSelectorOnMainThread:#selector(didChangeCloudStatus) withObject:nil waitUntilDone:NO];
Yet Xcode persists on giving me:
warning: Semantic Issue: Method '-performSelectorOnMainThread:withObject:waitUntilDone:' not found (return type defaults to 'id')
Of course the code compiles and runs fine, but I don't like the warning. When I redeclare the delegate like this, the warning vanishes, but I don't like the workaround:
#property (nonatomic, assign) NSObject <AccountFeedbackDelegate> *delegate;
What am I missing? What did I do wrong?
Cheers,
EP
performSelectorOnMainThread:withObject:waitUntilDone: is declared in a category on NSObject in NSThread.h. Since your variable is of type id, the compiler cannot be certain that it can respond to a message defined for NSObject. And unlike with plain id variables, the compiler warns you about this when your variable is declared id <SomeProtocol>.
So you should indeed declare your delegate as NSObject <AccountFeedbackDelegate>.
PS: The "standard" way to get rid of this kind of warning by declaring the protocol as #protocol AccountFeedbackDelegate <NSObject> won't work here because performSelectorOnMainThread:withObject:waitUntilDone: is not declared in the NSObject protocol.

Do you have to declare overriden #property methods in the .h file?

Beginner question here. In the .h file of an objective c class..
If you have an #property int someVar; for example.. and you're actually going to write the setter method yourself in the .m file.. do you still have to declare that setter method in the .h file?
If you have some #property declarations in the .h file and you are writing the getters and/or setters yourself.. you don't have to #synthesize them, correct? And if you don't synthesize them, do you have to declare them in the .h file or does the fact that making them properties is sufficient?
No, you don't have to define them since they already are defined. Defining a property implies that there will be a setter method – unless that property is readonly.
Just replace the #synthesize with #dynamic and implement getter and setter yourself.