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 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! 😀
You want this:
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:
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)