The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,726 other followers

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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: