我是Objective-C的新手,所以我可能会错误地说这个.我已经创建了一个Class"Message",我将它实例化为:
Message *newmsg=[[Message alloc]init];
Run Code Online (Sandbox Code Playgroud)
当我访问我的内容时newmsg(例如):
NSString *junk=((Message *)[globDat.allMsgs objectAtIndex:i]).text
我想交换两个实例的内容.特别是因为班上有多个项目.例如,这个想法是这样的(伪代码)
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;
Run Code Online (Sandbox Code Playgroud)
编辑:我忽略了在Singleton中使用数组来保存多个Message实例.您可以在示例中看到有关访问内容的信息.
所以只是交换指针给我一个错误:"表达式不可分配"
我试过这个(其中allMsgs是Singleton中的数组):
GlobalData *globDat=[GlobalData getSingleton];
Message *newmsg=[[Message alloc]init];
newmsg=[globDat.allMsgs objectAtIndex:0];
[globDat.allMsgs objectAtIndex:0]=[globDat.allMsgs objectAtIndex:1]; //<--ERROR
Run Code Online (Sandbox Code Playgroud)
还有这个:
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
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
您需要做的第一件事是确保您的allMsgs属性包含一个实例NSMutableArray.然后只需这样做:
[globData.allMsgs exchangeObjectAtIndex:0 withObjectAtIndex:1];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
130 次 |
| 最近记录: |