Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fiskah

macrumors newbie
Original poster
Nov 6, 2008
27
0
I have a problem that I don't quite understand.

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

"error: can not use an object as parameter to a method"
"error: incompatible types in return"

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

From XMLParser.m:
Code:
- (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:
Code:
- (NSMutableArray)parseNodeData:(NSString *)data;
 
I believe you want:
Code:
- (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.
 
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.
 
Why can't the method return the actual String instead of just the pointer to the string?

That is my question...

Kevin
 
Why can't the method return the actual String instead of just the pointer to the string?

Efficiency.

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.
 
Why can't the method return the actual String instead of just the pointer to the string?

That is my question...

Kevin

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.
 
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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.