cocoa pointer types: the * is mandatory
Posted by jpluimers on 2010/12/21
Coming from a Delphi background I’m highly spoiled by the feature that you do noe need to explicitly mark each and every use of a reference, pointer or class type as being a pointer: they always are, so Delphi implicitly knows. No need for a ^reference marker there (yes, there are a few corner case exceptions to this rule).
The same holds for .NET languages: they know when a type is a reference type, no need for those extra characters.
The Objective-C compiler – used for building cocoa applications in xCode – doesn’t know, so you have to include a star whenever a type is to be used as a pointer.
For instance, this will yield a compiler error “Cannot use object as a parameter to a method“:
- (void)log:(NSString)stage
{
NSLog(@"%@", stage);
// error "Cannot use object as a parameter to a method" on the above line with NSLog call
}
Since the xCode IDE only indicates lines upon compiler errors (and not columns when showing the error), searching for the actual cause can be painful when you have long lines or many things happening on one line.
Especially since the error is on the line with the NSLog call, but is caused by the NSString stage parameter being of type NSString in stead of NSString*.
This compiles fine:
- (void)log:(NSString*)stage
{
NSLog(@"%@", stage);
}
Programming for Mac/iPad/iPod is fun, but my Mac experience of 15 years ago still holds: a lot less luxury in your development than when writing similar PC applications.
–jeroen






Leave a comment