Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 我可以将类的一个实例复制到另一个类实例吗?_Objective C - Fatal编程技术网

Objective c 我可以将类的一个实例复制到另一个类实例吗?

Objective c 我可以将类的一个实例复制到另一个类实例吗?,objective-c,Objective C,我是Objective-C的新手,所以我可能用词不对。 我创建了一个类“Message”,并将其实例化如下: Message *newmsg=[[Message alloc]init]; 当我访问newmsg的内容时,我会这样做(例如): NSString*junk=((Message*)[globDat.allMsgs objectAtIndex:i])。text 我想交换两个实例的内容。特别是因为班上有多个项目。 例如,这个想法是(伪代码) 编辑:我省略了关于在单例中使用数组来保存多个消息

我是Objective-C的新手,所以我可能用词不对。 我创建了一个类“Message”,并将其实例化如下:

Message *newmsg=[[Message alloc]init];
当我访问
newmsg
的内容时,我会这样做(例如):

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

我想交换两个实例的内容。特别是因为班上有多个项目。 例如,这个想法是(伪代码)

编辑:我省略了关于在单例中使用数组来保存多个消息实例的部分。您可以在关于访问内容的示例中看到这一点

所以仅仅交换指针就给了我一个错误:“表达式不可赋值”

我已经尝试过了(其中allMsgs是Singleton中的数组):

GlobalData*globDat=[globaldatagetsingleton];
Message*newmsg=[[Message alloc]init];
newmsg=[globDat.allMsgs objectAtIndex:0];

[globDat.allMsgs objectAtIndex:0]=[globDat.allMsgs objectAtIndex:1]// 您可能需要添加一个执行复制的方法。下面是一个例子:

@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
你的例子是:

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];

您可能需要添加一个执行复制的方法。下面是一个例子:

@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
你的例子是:

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];

不确定这是否是您的意思,如果您想交换已经创建的类实例,您只需要交换指向它们的指针。这就是伪代码的工作方式,但不需要对tmpmsg进行alloc init

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;
如果要复制消息,则必须为该类编写一个复制方法

编辑后

看起来您正在更改数组中的对象。它需要是一个NSMutableArray

使用
insertObject:atIndex:
removeObjectatIndex:
replaceObjectAtIndex:withObject
exchangeObjectAtIndex:withObjectAtIndex
对其进行操作

这将交换索引0和1中的消息

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

@jlehr忘记了exchange方法:)更新了我的示例以使用更高效的调用。

不确定这是否是您的意思,如果您想交换已创建的类实例,只需交换指向它们的指针即可。这就是伪代码的工作方式,但不需要对tmpmsg进行alloc init

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;
如果要复制消息,则必须为该类编写一个复制方法

编辑后

看起来您正在更改数组中的对象。它需要是一个NSMutableArray

使用
insertObject:atIndex:
removeObjectatIndex:
replaceObjectAtIndex:withObject
exchangeObjectAtIndex:withObjectAtIndex
对其进行操作

这将交换索引0和1中的消息

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

@jlehr忘记了交换方法:)更新了我的示例以使用更高效的调用。

您需要做的第一件事是确保
allMsgs
属性包含
NSMutableArray
的实例。然后简单地这样做:

[globData.allMsgs exchangeObjectAtIndex:0 withObjectAtIndex:1];

您需要做的第一件事是确保
allMsgs
属性包含
NSMutableArray
的实例。然后简单地这样做:

[globData.allMsgs exchangeObjectAtIndex:0 withObjectAtIndex:1];


这样交换指针很好,但如果您能够解释预期的最终结果,这会有所帮助。
topMsg
bottomMsg
在您的用户界面中有什么意义吗?顺便说一句,我几乎可以肯定这是以前问过的问题,并且得到了一个很好的答案。“我现在似乎找不到它,但如果你再多找一点的话,你可能会有点运气的。@JoshCaswell对UI没有什么意义,但它比我显示的要复杂一些,因为我有错误-我会编辑文章以更详细。顺便说一句,你不需要分配和初始化对象指针(消息*)如果要使用它来指向已经存在的对象。当您想要创建一个新的类实例时,只需要执行alloc init。可以始终将指针设置为nil,直到您准备好使用它,即Message*newmsg=nil;这样交换指针很好,但如果您能够解释预期的最终结果,这会有所帮助。
topMsg
bottomMsg
在您的用户界面中有什么意义吗?顺便说一句,我几乎可以肯定这是以前问过的问题,并且得到了一个很好的答案。“我现在似乎找不到它,但如果你再多找一点的话,你可能会有点运气的。@JoshCaswell对UI没有什么意义,但它比我显示的要复杂一些,因为我有错误-我会编辑文章以更详细。顺便说一句,你不需要分配和初始化对象指针(消息*)如果要使用它来指向已经存在的对象。当您想要创建一个新的类实例时,只需要执行alloc init。可以始终将指针设置为nil,直到您准备好使用它,即Message*newmsg=nil;难道没有一种复制“内容”的方法,这样我就不必在类发生变化时编辑方法了吗?老实说,编辑很简单,只限于copyFrom方法。也就是说,如果您更改了name、add或remove属性,则只需更改copyFrom方法中的逻辑。此外,如果您需要进行任何此类更改,从而使重构变得很简单,Xcode会抱怨。谢谢,但我认为jlehr的回答是最有效的。难道没有一种方法可以复制“内容”,以便在类发生更改时不必编辑方法吗?编辑确实很琐碎,仅限于copyFrom方法。也就是说,如果您更改了name、add或remove属性,则只需更改copyFrom方法中的逻辑。此外,如果您有任何此类更改需要进行重构,Xcode会抱怨。谢谢,但我认为jlehr的回答是最有效的。我编辑了我的文章,提供了更多详细信息-此解决方案不起作用,我认为这是我的错,因为没有提供足够的信息。@wayneh更新了我的回答以处理数组,这就是我认为你有问题的地方。我投票赞成你的答案,但我看到了jlehr的第一个答案。谢谢你的帮助-我希望我能投两个票。@wayneh没问题,jle