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

Discussion in 'iPhone/iPad Programming' started by Setts, Jul 8, 2010.

  1. macrumors newbie

    Joined:
    Jul 8, 2010
    #1
    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?
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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:
    Code:
    #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.
     
  3. thread starter macrumors newbie

    Joined:
    Jul 8, 2010
    #3
    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
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    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.
     
  5. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #5
    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...
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    I'm pretty sure a custom class has some other advantages...
     

Share This Page