Making the Move: C# to Objective-C [The Gotchas]

Matthew Campbell, July 22nd

iStock_000004857449XSmall.jpgHaving you been playing around with Objective-C for a while now but you feel like you just don’t get it?

It may well turn out that the problem with learning Objective-C is not the programming language itself. Part of the problem is that you are not just dealing with Objective-C, but also with C and C++; the Cocoa-Touch framework and XCode are probably all new to you.

The Mindset

As you are learning Objective-C, especially if you are coming from another programming language like C#, Visual Basic or Java, the biggest obstacle is learning the Apple way of doing things. You will need to UNlearn almost everything you know about how you are used to doing things.

You Need To Be Open

The sooner you stop looking for the C# ways of doing things in Objective-C the better. I cannot tell you how much time I wasted looking for a “DataSet” to use with the iPhone. The same holds true for things like event handling that you may think you are an expert in.

You are better off learning how to make the iPhone work by looking at learning the platform as if it was your first programming language.

More Gotchas

Here are some more things that you should know as you are making this transition.

C, C++ and Objective-C – OMG What!??!?!

This is a big one, especially if you are the type of .NET or Java programmer that never had to learn the C programming language. On the iPhone you can use (and see as examples in blog posts) the C, C++ and Objective-C programming languages all used. Sometimes you will see them mixed and matched in the same file.

A case in point – if you look up how to use the SQLite (not framework, whatever they call it) you will see the examples using C. And the Apple engineers assume that you know where they are coming from.

This gets confusing when you are used to a strictly object-oriented language because even though everything is an object in Objective-C you can use work outside that paradigm in C.

Throw Out the Idea Of Strongly Typed Things

There is a sort of dogma with C# and Java about using strongly typed objects. That is – you must be explicit about what object you are referencing. This makes these languages easy to use because you can see exactly what you are working with because every programmer is forced to tell the system (and you by extension).

This does not exist in Objective-C and if you spend a lot of time trying to shoehorn Objective-C into a strongly typed paradigm then you will waste a lot of time. This makes certain situations seem ambiguous to newbies, but it also gives you a lot of flexibility. It also has some benefits when you think if of the idea of messaging and how things are implemented in Cocoa-Touch.

Manual Memory Management Will Cause You Pain

On the Windows desktop your programs do not usually crash because you did something wrong with the memory allocations (you probably don’t even know about this issue). This is because .NET and most modern programming languages use Garbage Collection – a automated system to deal with memory.

The iPhone does not and you will need to learn how to deal with this – 99% of bugs in my apps were due to memory problems in the early days. As soon as you get a basic understanding you need to start following a consistant set of rules when it comes to managing your app’s memory.

I recommend this video from the Mac Developer Network, Memory Management in Objective-C. It’s 14 bucks, but it will save you hours in debugging if you follow the advice there. As soon you learn enough to make your first prototype app buy this video…

You Must Know These Three Design Patterns

Learn about these three design patterns as soon as possible: Model-View-Controller, Delegation and Target-Action. Write down examples of how they are done in code and how they behave. If you do not you will end up chasing your tail for a month (which is what I did last year).

You can find information in a few places: I spend most of my e-book illustrating these topics using code as an example because they are so important.

Practice, Practice & Practice

As you probably already know as a programmer there is no substitute for practice. The learning curve for Objective-C seems high and you need to get used to playing in Apple’s sandbox. But, as you practice you will have the AHA moment and it will click. So, keep practicing and reading and watching videos – keep building prototypes and then throwing them out until you get it.

I want to know what the biggest GOTCHA you encountered when learning Objective-C! Comment below!