In the following example, does stringWithString:(NSString *)
copy the memory address/location of theName
to name
or it actually copies the data from theName
to name
?
@interface AddressCard:NSObject
-(void)setName:(NSString *)theName;
@end
@implementation AddressCard
NSString *name;
-(void)setName:(NSString *)theName
{
if(name!=theName)
name = [NSString stringWithString:theName];
}
@end
If I change the code to following, what does copy
do differently?
@interface AddressCard:NSObject
@property (copy, nonatomic) NSString *name;
@end
@implementation AddressCard
@synthesize name;
@end
In general, does copy
(@property
attribute) copy the address of the data or copies the data from one variable to another? If it is latter case, are we not consuming a lot of memory when the variable represents large data?
Thank you for your time and response!