Is typing strong in properties really necessary? [duplicate] - ios

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 *".

Related

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

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.

synthesizing properties with trailing underscore [duplicate]

This question already has answers here:
How does an underscore in front of a variable in a cocoa objective-c class work?
(9 answers)
Why rename synthesized properties in iOS with leading underscores? [duplicate]
(4 answers)
Closed 8 years ago.
I'm following along with a Lynda.com OCUnit testing tutorial that uses XCode 4. In one of the demos, it synthesizes properties in a Location.m file like this
#import "Location.h"
#implementation Location
#synthesize locationManager=locationManager_;
#synthesize speed=speed_;
The properties have a trailing underscore. In other tutorials I've followed (such as the Stanford iOS class), the synthesized properties usually are prefixed with an underscore for the instance variables.
When the properties are created in the .h file, there are no underscores.
#property (nonatomic, strong) CLLocationManager *locationManager;
#property float speed;
Why the trailing underscore in the synthesize statement?
By writing
#synthesize locationManager=locationManager_;
you are defining an ivar locationManager_which backs up your property.
So, to the property you still refer using
self.locationManager
However, ivar, "behind" that property is now called: locationManager_
#synthesize allows you to specify the name of the ivar. The property is named locationManager but the ivar is named locationManager_.

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

Objective-C NSString Category Getter/Setter Crash [duplicate]

This question already has answers here:
Objective-C: Property / instance variable in category
(6 answers)
Closed 9 years ago.
I'm trying to create NSString Category but the app crashes when trying to access getters/setters.
#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>
#interface NSString (MyCat)
#property (assign, nonatomic) ABRecordRef personRef;
- (void)setPersonRef:(ABRecordRef)personRef;
- (ABRecordRef)personRef;
#end
Can anyone point out the problem?
You cannot add properties to a class via a category. Adding methods is allowed because it doesn't increase the size of the class. Properties don't just add a getter and setter method, they also add a field to your class. The best way to add properties/fields to an existing class is to subclass it.

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