can not use an object as parameter to a method

Discussion in 'iOS Programming' started by fiskah, May 26, 2009.

  1. fiskah macrumors newbie

    Nov 6, 2008
    I have a problem that I don't quite understand.

    The following code fails to compile because of the following two errors:

    If I change the method to not return anything (void) it works fine. How come?

    From XMLParser.m:
    - (NSMutableArray)parseNodeData:(NSString *)url {
    	NSURL *urlObj = [[NSURL alloc] initWithString:url];
    	NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:urlObj];
        self.nodes = [[NSMutableArray alloc] init];
        [parser setDelegate:self];
        [parser setShouldProcessNamespaces:NO];
        [parser setShouldReportNamespacePrefixes:NO];
        [parser setShouldResolveExternalEntities:NO];
        [parser parse];
    	NSLog(@"Proceeded from parser.");
        [parser release];
    	return self.nodes;
    From XMLParser.h:
    - (NSMutableArray)parseNodeData:(NSString *)data;
  2. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    I believe you want:
    - (NSMutableArray *)parseNodeData:(NSString *)data;
    Maybe it's time to step back from the real code and review the basics of Objective-C coding before you continue.
  3. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    If you studied some C before trying Obj-C, you would realize that an object and a pointer to an object are two very different things. The "*", in certain places, tells you which you are dealing with.
  4. kteman macrumors newbie

    Oct 16, 2010
    Why can't the method return the actual String instead of just the pointer to the string?

    That is my question...

  5. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset

    To understand that answer, you have to understand that the memory pointed at by an object pointer contains several pointers and other data structures, which would have to be copied from one memory address to another memory address. Recall that a pointer is a memory address, so every time a pointer is passed or used, the actual code being executed is doing something with the values being pointed at, not with the actual pointer value itself. In other words, the pointer is just a more efficient way of referring to (or referencing) the in-memory object's data fields of interest.

    For an analogy, consider my one-word answer "Efficiency". If you already understood things like the in-memory structure of objects, and the speed costs of copying structures around, then that one-word reference would "point" you to the information for understanding why pointers are used. In other words, I wouldn't have to explain any of this, nor even provide links to a Wikipedia article (links being another form of "pointer") on computationally efficient data representations.
  6. admanimal macrumors 68040

    Apr 22, 2005
    Objective C objects must always be referenced by pointer. You cannot pass or return objects by value.

    As chown33 pointed out, there are good reasons for this.
  7. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Efficiency is not the reason that Objective C objects cannot be returned from functions. The rule is that all Objective-C objects must be created on the heap. It's not allowed to create them on the stack. This prevents returning a NSMutableArray. Only NSMutableArray* can be returned.

    Why can't Objective-C objects be allocated on the stack? Don't know for sure. But this is a historic fact from the beginning of Objective-C. It probably has to do with the philosophical objection to automatic calling of constructors/destructors/copy constructors and other similar function calling that happens behind the developer's back and are important parts of C++. Allocating objects on the heap is in some ways simpler and that's part of the Objective-C philosophy.
  8. admanimal macrumors 68040

    Apr 22, 2005
    I think the main reason has to do with memory management. For one thing, calling retain/release on an object on the stack doesn't really make sense. Stack allocation also means you need to know the exact size of things at compile-time, and I'm not sure that is compatible with how things work in Objective-C.

    That said, there is one type of object that is allocated on the stack as of OS X 10.6 and iOS 4: blocks.

Share This Page