< Back

C# to iOS, Beginner&#8217;s Guide to Method Syntax

I’m a self-proclaimed late-adopter and commitment-phobe, particularly when it comes to consumer electronics. So, as one would assume, I was quite late to the iPhone party, picking up the 3GS when the 4 was nearly out. Also, I am far more comfortable building web applications than desktop or native mobile applications and so I naturally kept a healthy distance from the iOS SDK.

This begs the questions, why on earth then would I start iPhone mobile development? In short, I contracted a crazy infection in my face and I had to keep my mind distracted with something even more painful, Objective C. With that said, the best way to achieve said pain is to open up XCode and get rolling.

Syntax

Syntactically, Cocoa was shock to my abstracted innards. It vaguely resembles Java until it kicks you across the face with faceted methods calls and square-bracket mines.

Methods

Arguably, the oddest parts of Objective C syntax, are method names and particularly, multi-parameter methods. A standard instance method with no parameters looks like this:

- (void)methodName { }

Pretty simple, but don’t forget the minus sign, which signals that it’s an instance method and not a class method. If you’re returning a value, change (void) to the return value’s type.

You can call the above method via:

[object methodName];

Multi-part Method Names

Here the fun starts. Let’s say we have an instance method which takes two parameters, a date and an integer, and then returns a changed date.

- (NSDate *)incrementDate:(NSDate *)date byDays:(int)days { }

Woah, coming from a Java world, this looks like a complete nightmare. Is the part before the (type) the actual parameter name? If so, where the heck is the method name? Rather intuitively, or not, the part before the (type) is used in the method call and the part following is the block-scoped variable used within the method. The above example would be called like:

NSDate * newDate = [object incrementDate:dateObject byDays:7];

You might be asking, what’s the deal with that dang asterisk? Maybe not, but I certainly did. The asterisk means that the variable is a pointer to an object. In C#, the equivalent would be:

Object obj = new Object(); // obj is pointing to the Object object.

Just a taste

Obviously this is a just a sliver of what’s in store for me as I venture down the iOS road. Next up, interfaces and those funky “.h” files.

Connect with us

Thank You!

We really appreciate your interest in what we do.

We'll get back to you as soon as we can.