0

I have the following problem, because i just have started coding objective-c.

I have some instance with

@interface MyClass
    @property (strong) MySecondClass *first;
    @property (strong) MySecondClass *second;
@end

@implementation MyClass
    @synthesize first = _first;
    @synthesize second = _second;

    /*this is called while initialisation*/
    -(void) initialisation{
        _first = [[MySecondClass alloc] init];
        _second = [[MySecondClass alloc] init];
     }

    /*what i want to do*/
    -(void) swap{
        // second one should be replaced with first one
        _second = _first; 

        // first pointer should be replaced by new instance
        _first = [[MySecondClass alloc] init];
    }

@end

Problem is, that when i call the swap method, the _first still points to the old object so _second will be replaced by same new object.

I already tried copy like follows, but it throws an exception.

 _second = [_first copy];

PS: Im using ARC

Edit

What i really would like to accomplish, would be something like:

 _second = _first
 MySecondClass *tmp = [[MySecondClass alloc] init];
 _first = &tmp;

But this shows compiler error: Implicit conversion of an indirect pointer to an Objective-C pointer to 'MySecondClass *' is disallowed with ARC

Thanks in advance.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Dom
  • 658
  • 6
  • 10
  • what kind of exception is it throwing? the console should reveal that. – Michael Dautermann Nov 24 '12 at 15:05
  • The exception: Uncaught exception: -[MySecondClass copyWithZone:]: unrecognized selector sent to instance 0xf305570 I dont't want to copy the memory, just the pointer to it. The _first pointer then should point to a new object. – Dom Nov 24 '12 at 15:07
  • I always get this kind of problem and I ended up with writing copyWithZone. There is something called deepcopy ans shallowcopy the reason behind this. – Anoop Vaidya Nov 24 '12 at 17:21
  • 1
    @AnoopVaidya: No, the distinction between deep and shallow copying of objects has nothing to do with swapping the values of two variables. – Peter Hosey Nov 24 '12 at 18:13
  • @Dom - the first `-(void)swap{` does what you want. It is not clear what your problem is – hooleyhoop Nov 24 '12 at 18:17
  • @hooleyhoop: No, the original implementation of `swap` does not swap the variables. – Peter Hosey Nov 24 '12 at 23:17
  • @peterhosey does your answer swap the variables? – hooleyhoop Nov 25 '12 at 01:36
  • @hooleyhoop: Yup. (Strictly speaking, no, since it's not swapping the existing values, but rather swapping the value from `_first` into `_second` before creating the new object to store into `_first`. A more precise description would be that it pushes a new object into a two-element queue; if there was already a second object, it falls off when the former first becomes the new second. But that's what the questioner asked for.) – Peter Hosey Nov 25 '12 at 03:16
  • @peterhosey ok, and that is different than the first `-(void)swap{` how? – hooleyhoop Nov 25 '12 at 11:01
  • @hooleyhoop: I explained the difference in my answer. – Peter Hosey Nov 25 '12 at 17:44

2 Answers2

0

My newer answer:

You should also make certain the class you're trying to copy implements the "NSCopying" protocol. For example, UIViewController doesn't implement it and so it doesn't respond to calls to "copy" or "copyWithZone".

Try resetting that pointer a different way than using "copy".

My original answer:

try referring to the properties via their accessors.

e.g.:

self.second = self.first;

self.first = [[MySecondClass alloc] init];

etc.

The only place you must work with "_second" & "_first" directly is in the class init method.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Thanks, I tried that already, but it's the same. By the way, i didn't replaced the setters and getters. – Dom Nov 24 '12 at 15:16
  • 1
    The point is, that i really DON'T want to COPY the INSTANCE. I just want to swap pointers of one INSTANCE. I got so far that the _second pointer points to the INSTANCE, but now i want to replace the _first pointer to a NEW one. – Dom Nov 24 '12 at 15:28
-1

What i really would like to accomplish, would be something like:

_second = _first
MySecondClass *tmp = [[MySecondClass alloc] init];
_first = &tmp;

But this shows compiler error: Implicit conversion of an indirect pointer to an Objective-C pointer to 'MySecondClass *' is disallowed with ARC

That's because the values of _first, _second, and tmp are already pointers, so “&tmp” is a pointer to a pointer—an “indirect pointer” (not especially clear wording on the compiler authors' part).

The solution there is to not say &tmp. You just want tmp here.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • I found out that my problem is a little more complex than my simplified example. You are right, my swapping function does what it's supposed to do, but it doesn't solve my complex problem. I need to look deeper into how viewcontrollers work. Thanks anyways. – Dom Nov 25 '12 at 17:15