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
stronginstead ofretain. And yes, you should still usecopyforNSStrings. The use ofcopyhas nothing to do with ARC; you wantcopybecause if someone assigns anNSMutableStringto your property you don’t want the string changing behind your back. Usingcopygives 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:
The
NSStringcould be declared asstrongas well, butcopyis almost always preferable for strings (and really any immutable type that has a mutable variant, e.g. arrays, dictionaries, etc).