The following code is on apple's website.
1) Inside the setMyArray
method, is it necessary to release myArray
before setting a new value to it? I thought setting the value of an object to a new object, will release the old object from memory.
2) why does it say myArray = [newArray mutableCopy];
, instead of simply saying myArray = newArray;
? Is it necessary to pass a mutable copy to our property?
@interface MyClass : NSObject {
NSMutableArray *myArray;
}
@property (nonatomic, copy) NSMutableArray *myArray;
@end
@implementation MyClass
@synthesize myArray;
- (void)setMyArray:(NSMutableArray *)newArray {
if (myArray != newArray) {
[myArray release];
myArray = [newArray mutableCopy];
}
}
@end
EDIT: Would it be the same if myArray was (nonatomic, retian)
Apple Documentation
copy
Specifies that a copy of the object should be used for assignment. (The default is assign.)
**The previous value is sent a release message**.