1

I'm new to Objective-C, so I may be phrasing this incorrectly. I've created a Class "Message" and I instantiate it like this:

Message *newmsg=[[Message alloc]init];

When I access the contents of newmsg I do this (for example):

NSString *junk=((Message *)[globDat.allMsgs objectAtIndex:i]).text

I want to swap the contents of two instances. Especially because there are multiple items in the class. For example the idea is this (pseudocode)

Message *TopMsg=[[Message alloc]init];
Message *BottomMsg=[[Message alloc]init];
Message *tmpmsg=[[Message alloc]init];
...
//enter values for TopMsg.xyz and BottomMsg.xyz
....
//swap
tmpmsg=TopMsg;
TopMsg=BottomMsg;
BottomMsg=tmpmsg;

EDIT: I left out the bit about using an array in a Singleton to hold multiple Message instances. You can kind of see it in the example about accessing the contents.

So just swapping pointers gives me an error: "Expression not assignable"

I've tried this (where allMsgs is the Array in the Singleton):

GlobalData *globDat=[GlobalData getSingleton];
Message *newmsg=[[Message alloc]init];
newmsg=[globDat.allMsgs objectAtIndex:0];
[globDat.allMsgs objectAtIndex:0]=[globDat.allMsgs objectAtIndex:1]; //<--ERROR

and this:

GlobalData *globDat=[GlobalData getSingleton];
Message *newmsg=[[Message alloc]init];
newmsg=[globDat.allMsgs objectAtIndex:0];
(Message *)[globDat.allMsgs objectAtIndex:0]=(Message *)[globDat.allMsgs objectAtIndex:1]; //<--ERROR

How can I do this?

4
  • 1
    Swapping pointers like that is just fine, but it would help if you can explain your intended end result. Do topMsg and bottomMsg have some significance in your UI?
    – jscs
    Mar 14, 2012 at 20:24
  • BTW, I'm nearly certain this has been asked before and got a good answer. I don't seem to be able to find it at the moment, but you might have some luck if you poke around a bit.
    – jscs
    Mar 14, 2012 at 20:33
  • @JoshCaswell No significance to the UI, but it's a bit more complex than I've shown as I get errors - I'll edit the post to be more detailed.
    – wayneh
    Mar 14, 2012 at 20:54
  • 1
    As an aside, you don't need to alloc and init an object pointer (Message*) if you going to use it to point to an object that already exists. Only need to do alloc init when you want to create a new class instance. Can always set the pointer to nil until your ready to use it i.e. Message *newmsg = nil;
    – Ben
    Mar 14, 2012 at 21:23

3 Answers 3

5

The first thing you need to do is make sure your allMsgs property contains an instance of NSMutableArray. Then simply do this:

[globData.allMsgs exchangeObjectAtIndex:0 withObjectAtIndex:1];
2
  • Thanks! I knew it had to be easy.
    – wayneh
    Mar 15, 2012 at 13:33
  • UPDATE: exchangeObjectAtIndex does NOT work when using a combination of UITableView and NSMutableArray. See this post by 'WiZ': stackoverflow.com/questions/486289/…
    – wayneh
    Mar 28, 2012 at 13:38
2

Not sure if this is what you mean, if you want to swap class instances that have already been created, you can just need to swap the pointers to them. This is how your pseudo code work, but you don't need to alloc init the tmpmsg.

Message *TopMsg=[[Message alloc]init];
Message *BottomMsg=[[Message alloc]init];
Message *tmpMsg=nil;
...
// Set values in topmsg and bottommsg
...

tmpMsg=TopMsg;
TopMsg=BottomMsg;
BottomMsg=tmpmsg;

If you want to copy Messages then you'll have to write a copy method for the class.

AFTER EDIT

It looks like you are changing objects in an array. It needs to be an NSMutableArray.

Use insertObject:atIndex:, removeObjectatIndex:, replaceObjectAtIndex:withObject and exchangeObjectAtIndex:withObjectAtIndex to manipulate it.

This will swap messages in indices 0 and 1

GlobalData *globDat=[GlobalData getSingleton];
[globDat.allMsgs exchangeObjectAtIndex:0 withObjectAtIndex:1]

@jlehr forgot about the exchange method :) Updated my example to use your more efficient call.

4
  • I've edited my post with more detail - this solution doesn't work and I think it's my fault for not providing enough info.
    – wayneh
    Mar 14, 2012 at 21:01
  • @wayneh Updated my answer to deal with arrays, which is where I think your having the problem
    – Ben
    Mar 14, 2012 at 21:25
  • I voted up your answer but I saw jlehr's first. Thanks for the help - I wish I could vote for both.
    – wayneh
    Mar 15, 2012 at 13:35
  • @wayneh No probs, jlehr answer was the best way to swap objects in an array
    – Ben
    Mar 15, 2012 at 14:30
0

You may want to add a method that performs a copy. Here's an example:

@interface Message
@property (nonatomic, copy) NSString *x;
@property (nonatomic, copy) NSString *y;
@property (nonatomic, copy) NSString *z;
@end

@implementation Message
@synthesize x;
@synthesize y;
@synthesize z;
-(void)copyFrom:(Message *)message
{
   self.x = message.x;
   self.y = message.y;
   self.z = message.z;
}
-(void)dealloc
{
  self.x = nil;
  self.y = nil;
  self.z = nil;
  [super dealloc];
}
@end

Your example:

Message *TopMsg=[[Message alloc]init];
Message *BottomMsg=[[Message alloc]init];
Message *tmpmsg=[[Message alloc]init];

TopMsg.x = @"Foo1";
TopMsg.y = @"Bar1";
TopMsg.z = @"Boo1";

BottomMsg.x = @"Foo2";
BottomMsg.y = @"Bar2";
BottomMsg.z = @"Boo2";

[tmpmsg copyFrom:TopMsg];
[TopMsg copyFrom:BottomMsg];
[BottomMsg copyFrom:tmpmsg];
3
  • Isn't there just a way to copy the 'contents' so that I don't have to edit a method if the Class changes?
    – wayneh
    Mar 14, 2012 at 20:25
  • Editing is trivial honestly and confined to the copyFrom method. That is to say, if you change name, add, or remove property, you need only to change the logic in 1 spot, the copyFrom method. Besides, Xcode will complain if you have any such changes to make which makes refactoring a snap.
    – Jeremy
    Mar 14, 2012 at 20:38
  • Thanks, but I think jlehr's answer is most efficient.
    – wayneh
    Mar 15, 2012 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.