PDA

View Full Version : Obj-C for iPhone: Having difficulties with returning a "struct" from method...




Setts
Jul 8, 2010, 02:32 PM
I am in the process of developing my 1st iPhone App. I am doing well just from the documentation available from Apple but I can't seem to get by my latest roadblock. I pass an argument to a method and want it to return some calculated related data in a "struct"ure. I have used typedef and struct to define this data in the method. I have used NSLog to verify that the data elements are indeed populated correctly. I would like to have a pointer to the structure returned by the method. How do I do that and then relate the same struct in the calling (messaging?) method?



lee1210
Jul 8, 2010, 02:45 PM
The definition of your struct and typedef will need to be in a .h file that is included anywhere that this method can be used. From there, you'll need to declare a pointer to this type of struct in your method, then allocate memory the size of your structure and assign the pointer to this memory to the pointer you declared. You can then fill in the struct and return the value of the pointer. It would go something like this:

#include <stdio.h>
#include <stdlib.h>

struct myInfo {
int doubled;
int tripled;
double split;
int squared;
};

typedef struct myInfo myInfoType;

myInfoType *infoOfNum(int);

int main(int argc, char *argv) {
myInfoType *infoOne = NULL;
infoOne = infoOfNum(17);
printf("Double: %d\tTriple: %d\tSplit: %f\tSquared: %d\n",infoOne->doubled,infoOne->tripled,infoOne->split,infoOne->squared);
free(infoOne);
}

myInfoType *infoOfNum(int x){
myInfoType *localInfo = NULL;
localInfo = malloc(sizeof(myInfoType));
if(!localInfo) {
fprintf(stderr,"Could not allocate memory");
exit(-1);
}
localInfo->doubled = x*2;
localInfo->tripled = x*3;
localInfo->split = x/2.;
localInfo->squared = x*x;
return localInfo;
}


This is plain C, but it should be easy to adapt. The real problem with code like this is that infoOfNum returns a pointer to memory that needs to be free'd. This is no fun, in most cases. It would probably better to accept a pointer to this struct type and fill it in in the function. That way the caller can pass the address to a local struct, or allocate themselves knowing that they must free it themselves later. The only reason i wrote it this way was because you said you wanted to return a pointer to the struct.

-Lee

EDIT: Any reason your not just using a class with instance variables to communicate this information? Structs have their place, but memory management will be much easier if you use an object for this.

Setts
Jul 8, 2010, 02:53 PM
Lee: Thanks for the quick input. I'll review your example but I think your right that I should pass a pointer to a struct in the calling method. Thanks again. I'll let you know how I fare. John

dejo
Jul 8, 2010, 04:08 PM
...but I think your right that I should pass a pointer to a struct in the calling method.
I would suggest returning a pointer to a custom class' object instead. This is much more in line with how things in Objective-C tend to be done.

firewood
Jul 8, 2010, 05:32 PM
A custom class object is implemented in iOS as a C struct with some hidden fields. So if you don't mind the "wasted" bytes...

dejo
Jul 8, 2010, 06:43 PM
A custom class object is implemented in iOS as a C struct with some hidden fields. So if you don't mind the "wasted" bytes...
I'm pretty sure a custom class has some other advantages...