Making the Move: C# to Objective-C [Syntax]

Matthew Campbell, July 21st

iStock_000009011981XSmall.jpgAre you a C# or Java programmer who is trying to take the plunge into the iPhone/Mac development world?

That was me too a little over a year ago; I tried my hand at Objective-C thinking it would be a snap since I was already so proficient at C# and the whole family of .NET products. Plus, Steve Job’s presentation about XCode made programming for the iPhone seem so easy and sexy.

How Different Could Objective-C Really Be?

If you are interested in reading this it is because you probably already know that it is VERY different. It seems like it shouldn’t be: the iPhone is essentially a client technology, Objective-C is an object-oriented programming language and Cocoa-Touch is simply a framework.

This week I am going to be a talking about Objective-C what tips you need to know about the language. Because even though the videos make it all look easy with the syntax coloring, the shiny XCode IDE and the Steve Jobs halo effect learning how to work with Objective-C can be very disorienting for newbies. In particular, it is disorienting for people who already know a programming language.

It is a also a very rewarding experience to work with Objective-C once you get the hang of it – so keep with it!

And Then There Are the Square Brackets

This is probably the one thing that scare people off the most about Objective-C. Here is an example of something you would do using the square brackets of Objective-C:

[alert displayWithThisMessage:@"I say this!"];

Look at the above statement very closely. In C# (and I think Java) it would look something like this:

Alert.DisplayWithThisMessage("I say this!");

This difference seems minor when you are looking at it in a blog entry – it is when you are hacking on it at 3AM and attempting to use nested methods that it messes with your head.

Here Are the Pieces of Syntax

Again, consider the statement:

[alert displayWithThisMessage:@"I say this!"];

A Line of Code

The first thing you should notice is that the entire statement is surrounded in square brackets ([,]) and the line of code ends with a semi-colon (;).

Enter the Object Stage Left

The next thing is the object; Objective-C is a very object-oriented language and when you are dealing purely with Objective-C you can assume everything is an object. The object in this statement is alert.

The Method is the Message!

In Objective-C you will not be calling an object’s method, but you will be sending a message to an object. This has implications that you will run into as you get more advanced. In the above statement the message is displayWithThisMessage.

Essentially, this is how you get objects to do stuff in Objective-C.

Parameters

You send parameters as part of the message. In the above line parameter comes right after the colon – it is @”I say this!”. If you have more than one parameter to send in the message you get to add descriptive text before hand like in the following example:

[myObject aMethodWithTwoParameters:@"First Parm"

aNumberToDisplay:2];

The descriptive text here is aNumberToDisplay.

What Do You Think About the Objective-C Syntax?

This is just a taste of how to construct statements in Objective-C; there are some other things that I will be sharing tomorrow that I hope will help you get into the Objective-C pattern. The biggest hurdle with Objective-C is not syntax, but mindset.

What is the most surprising syntax difference that you have found as you have been learning Objective-C?

[...] sources: How to make iPhone apps & [...]

  • Hours of videos, demos & screen casts
  • SOURCE CODE
  • Detailed HANDS-ON Exercises
  • Much Much Much More
  • UIKit
  • Objective-C
  • Core Data
  • More!
  • Learn How To Make An iPhone App right now using my proven system.

    PS: Tons of us are having a blast living off our own code, join us now. Click here to get all the details.