iphonensstringcopycopywithzone

copy one NSString to another


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!


Solution

  • your object should probably implement the copy itself:

    @implementation MyData
    
    -(id)copyWithZone:(NSZone *)zone
    {
        MyData *obj = [[[self class] alloc] init];
        obj.uID = self.uId;
        obj.name = self.name
        ...
        return obj;
    }
    
    ....
    @end