开发者

How to return an instance of a class in Objective C?

I'm trying to return an instance of a class in a function, but I'm not sure how to do it.

I have a class called GenerateMoves, and an instance of that class is created:

GenerateMoves *move = [[GenerateMoves alloc] init];

I then have another class which has to开发者_如何学JAVA use the variables which are set up in that instance of the class ("move", from above).

So I created a function:

-(Class)returnClass {
return move;
}

And then in the other class:

GenerateMoves *move = [otherClass returnClass];

However, it throws a warning saying it is an incompatible pointer type.

How do I do this?

Thanks for your assistance! :D


You want this:

-(GenerateMoves*)returnClass {
  return move;
}

Although I would call it returnMove, rather than returnClass

EDIT: A bit more detail.

While your way of writing things is perfectly valid Objective C, I think it's easier to understand if you write it like this:

GenerateMoves* move = [[GenerateMoves alloc] init];

in other words, you're creating a pointer to a GenerateMoves instance and assigning it to the variable move. It then follows naturally that the return type of returnClass is GenerateMoves* (again a pointer to a GenerateMoves instance)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜