What is the default property of a #property in objective-c? [duplicate] - ios

This question already has answers here:
What are the default attributes for Objective-C properties?
(3 answers)
Closed 7 years ago.
A #property can be set as strong, weak , assign , copy ... like
#property (copy, nonatomic) NSString *string;
#property (strong ,nonatomic) CustomClass *object;
#property (weak,nonatomic) id <CustomDelegate>delegate;
However, if
#property id <CustomDelegate>delegate; weak?strong?
#property (copy, nonatomic) NSString *string; strong?
If (weak,nonatomic) is abbreviated. What is the default value of an id? And other?

Properties Are Atomic by Default
This means that the synthesized accessors ensure that a value is always fully retrieved by the getter method or fully set via the setter method, even if the accessors are called simultaneously from different threads.
Because the internal implementation and synchronization of atomic accessor methods is private, it’s not possible to combine a synthesized accessor with an accessor method that you implement yourself. You’ll get a compiler warning if you try, for example, to provide a custom setter for an atomic, readwrite property but leave the compiler to synthesize the getter.
For more detail you can read this article https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html
Thanks

id is a reference to some random Objective-C object of unknown class, thus it's default attributes are:
#property (atomic, readwrite, strong) id value;
Note: delegates 99.999% of the time should be weak.

Related

Xcode generates some remove methods for my property, what are they?

When I define a property like:
#property (nonatomic, strong) NSObject *myTestObject;
Xcode would automatically generate 4 methods, all begins with "remove":
So what is this mechanism, and how to use it?

Is typing strong in properties really necessary? [duplicate]

This question already has answers here:
Objective-C ARC: strong vs retain and weak vs assign
(8 answers)
Closed 9 years ago.
#property (nonatomic, strong) NSString *dude;
#property (nonatomic) NSString *dude;
#property (nonatomic, weak) NSString *dude;
What exactly is the difference between these 3?
There's no difference between the first and second, as "strong" is the default & implicit attribute.
The third uses a weak reference, which means that when the object is released by the last owner, the dude property becomes nil.
Oh, lastly, NSString properties should actually be declared with "NSString *".

iOS SDK - Correct methodology in making connections with an outlet?

I know that if I use
#interface TPN : UIViewController{
IBOutlet UIView *testView;
}
#property (strong, nonatomic) IBOutlet UIView *testView;
I know that the first one is essentially a private variable that is only accessed within the class. and the second one "#property" is able to be accessed from an instantiated object. I find it odd in most tutorials that people tend to set properties when they are usually changing an outlet from within the class itself. Is there any guideline I should be following?
You no longer need to specify the ivar at all. Nor is there a need to use #synthesize.
Use a property, but make sure it is weak, not strong
#interface TPN : UIViewController
#property (weak, nonatomic) IBOutlet UIView *testView;
In the implementation you can now access the ivar as _testView.
For a private property (above is public) instead put the #property within a category in the implementation file:
#import "TPN.h"
#interface TPN ()
#property (weak, nonatomic) IBOutlet UIView *testView;
#end
#implementation TPN
....
You are right. If you are only going to use the instance variable inside the class, there is no point to make it a property. A property simply means a pair of getter/setter. If you don't need to do anything specially when getting/setting, you can just use the instance variable directly.
Prior to ARC, there was a side benefit to using properties everywhere, in that it was easier to do memory management for object pointers -- properties declared as retain would automatically release and retain for you when you set it, without you typing that code yourself like you would have to do with an instance variable directly. However, now in ARC, the compiler does that for you anyway when you assign, if it's a strong instance variable. So this benefit is now irrelevant.
I would suspect that a lot of the reason people use properties for outlets is because of perpetuation of that usage in almost all tutorials and examples (possibly due to the previous benefit with retain memory management), and they simply don't realize they can do something different.

IOS, ARC, Property: (readwrite, nonatomic) vs (radwrite, retain, nonatomic)

I am have read up some tutorials on ARC and am still left a bit confused on properties declarations. I wrote most most my code using the following pattern:
#property (readwrite, nonatomic) PlayerData* playerData;
#property (readwrite, nonatomic) MusicLayer* musicLayer;
#property (readwrite, nonatomic) bool isPowerUpAvailable;
Now that I finally started to deal with memory leaks XCode suggested me that in some bits of code I should have added the "retain" keyword in the property declaration.
Using ARC I thought I shouldn't "Bother" about retain counts anymore. Is there some concept I am not getting or missing? Any tutorial references or explanation would be greatly appreciated.
ARC is will retain object based on the property declaration, you should use strong for properties that need to be retained and weak for properties that do not need to be retained.
weak properties are also nilled when the object is deallocated.
The compiler will always assume that properties are readwrite so there is no need to declare then this way.
#property (strong, nonatomic) PlayerData* playerData;
#property (strong, nonatomic) MusicLayer* musicLayer;
// Need use assign since strong is for objects only.
#property (assign, nonatomic) bool isPowerUpAvailable;
If you prefer continue to use your code, you can exclude ARC only on the specific file .m you want:
Go to Targets > Build Phases > Compile Sources and select your .m file double click on right column of the selection and add -fno-objc-arc so you are exclude ARC only a selected file.
Or if you want to convert all application to new code with ARC, after make a Backup of you project, go to:
Edit > Refactor > Convert to Objective-C ARC and after this do the same but click on Convert to modern Objective-C Sintax
here the screen:
Pay attention not always working before to try duplicate your project!
Hope this help you

Should I continue to use iVar and #property (nonatomic, retain) plus #synthesize under Automatic Reference Counting (ARC)?

Like a doop I'd been declaring Instant Variables (iVar) and then #property in the interface .h file for a while now.
#interface MainGameViewController : UIViewController {
UserFactorsViewController *userFactorsViewController;
UITableView *myTableView;
}
#property (nonatomic, retain) UserFactorsViewController *userFactorsViewController;
#property (nonatomic, retain) IBOutlet UITableView *myTableView;
Under Automatic Reference Counting, should I just dispense with iVar and go all #property? Should I even have the word "retain" in property? What if I'm deploying for iOS 4.3, should I still use ARC?
Don't feel like a doop, even though the compiler will add ivars for you if you don't include them, many people still declare them (many book authors as well) to make the code a little bit easier to read (easier to distinguish between ivar and property).
When creating a property now, Apple wants you to think in terms of Object Graphs, so do some research on "strong" and "weak" property attributes instead of retain and releases.
Also, iOS 4 is setup as a target for ARC so you should be ok. But I believe if you wanted to support iOS 3.0 you would have to manually manage retain and releases as before.

Resources