copy one NSString to another

yozhik picture yozhik · Nov 17, 2010 · Viewed 16.5k times · Source

How to copy one NSString to another?

@interface MyData : NSObject
{
    @private

    //user's private info
    NSInteger uID;
    NSString *name;
    NSString *surname;
    NSString *email;
    NSString *telephone;

    //user's picture
    UIImage *image;
}

@property (nonatomic, assign) int uID;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *surname;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *telephone;
@property (nonatomic, retain) UIImage *image;
@end

I have two objects of this type. MyData *obj1, obj2;

First is initialized. Second I want to initialize with first.

obj2 = [obj1 copy];   //crashes

    newData.surname = data.surname;   //crashes to
    newData.email = data.email;
    newData.telephone = data.telephone;

I neeeeed a COPY of second object NOT RETAIN!!! Help please! Thanx!

Answer

Alex Brown picture Alex Brown · Nov 17, 2010

you can use the NSString method stringWithString.

See also stringwithstring, what's the point? to understand when this might be preferred to simply giving it the same string.