I want to use ARC in my simple class where I store some values to pass into another class. And I want to know what reference I have to use in the property. To use it in ARC, I have this:
@interface MyItem : NSObject
@property (retain) NSString *valueID;
@property (retain) NSString *itName;
@property (retain) NSDate *creationDate;
@property (assign) float rating;
This is a very simple class, and I want to know how to use it in ARC. What reference do I have to use? Do I have to use a copy for the NSString etc?
EDIT:
If I have a UIViewController, and I want to use a property for NSString and for MyItem object like this:
@interface MyViewController : UIViewController
@property (nonatomic, retain) NSString *myString;
@property (nonatomic, retain) MyItem *newItem;
What reference do I have to use for NSString and for MyItem object?
You want to use strong
instead of retain
. And yes, you should still use copy
for NSString
s. The use of copy
has nothing to do with ARC; you want copy
because if someone assigns an NSMutableString
to your property you don't want the string changing behind your back. Using copy
gives you an immutable snapshot of the mutable string at the point where the assignment took place.
This is the recommended way to declare the properties in your view controller example:
@interface MyViewController : UIViewController
@property (nonatomic, copy) NSString *myString;
@property (nonatomic, strong) MyItem *newItem;
The NSString
could be declared as strong
as well, but copy
is almost always preferable for strings (and really any immutable type that has a mutable variant, e.g. arrays, dictionaries, etc).