0

I have a methode like this :

-(void)myMethod:(MyClass *)object {

self.object1 = object;
self.object2 = object;

....
}

and object1 and object2 are declared like this :

property (nonatomic, strong) MyClass *object1;
property (nonatomic, strong) MyClass *object2;

and MyClass is like this :

@interface MyClass :NSObject

....
@end

and later when I modify the self.object1 the self.object2 is modified to, I would like that self.object2 don't change its value when modifing self.object1

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
samir
  • 4,501
  • 6
  • 49
  • 76
  • Then don't assign the same value to both!! PLEASE, PLEASE spend some time learning about pointers and objects before you try to use Objective-C. – Hot Licks May 19 '13 at 22:58
  • i would like to save the old value, i thing you have not understand my problem, PLEASE, PLEASE, Understand question before answering. – samir May 19 '13 at 23:10
  • If you understood objects and pointers you'd never have needed to ask this question. – Hot Licks May 20 '13 at 02:08

2 Answers2

4

You need to make the two properties copy instead of sttrong. But then you need to make your MyClass conform to the NSCopying protocol and you must implement the copyWithZone: method.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
3

You don't have two objects that point to one.

You have two pointers that point to the same object.

So if you modify self.object1 by definition it will modify the self.object2

The only way to change this behavior is to have object1 and object2 point to two different objects.

Alan
  • 45,915
  • 17
  • 113
  • 134