Saturday, January 02, 2010

C# to iPhone Objective-C

This set of blog posts is more for my own memory than anything else. I’m playing with iPhone development and wanted to draw similarities as-well as write down things I learn whilst getting to grasp with Objective-C.

So here we go. Let’s start at the beginning by looking at some core differences between the 2. The order might be a bit rambling but that’s just me writing stuff down that drops into my head! Soon I’ll try get onto the more visual aspects of iphone development but I think it’s essential to get your head around how to structure objective-c code before diving into guis.

For the record I learnt a lot from Dr Google, and this website was especially good: http://www.otierney.net/objective-c.html

1. Almost everything is a pointer.

To ‘create’ an instance of an object in Objective-C we tend to have 2 choices. The first involves ‘allocating’ memory for the object then ‘initialising’ it in some way. The 2nd involves calling a factory (think static) method on a class.

Whichever we do what we end up with is a pointer to an object. In Objective-C we must explicitly show this by using the * character. So to create an NSString object (a fairly core object) we can say:

NSString* myString = [NSString alloc];

more on the syntax in a minute.

To create the string object with a value:

NSString* myString = [[NSString alloc] initWithString: @”Hello” ];

Or you can say:

NSString* myString = [NSString stringWithString: @”Hello”];

The 1st two use instance methods on the NSString to initialize it. The 2nd uses a factory method to create the string. The main difference between the 2 is that by explicitly allocating the string (the 1st way) you are responsible for releasing the memory. With the 2nd way you aren’t. I’ll leave the specifics for another post as it deserves more time!

2. Calling a method on an object.

In C# / Java this is done via the dot (.) notation. So to call method Foo on object bar we say bar.Foo(). If we want to pass parameters to this method call we put them inside the parenthesis. So bar.Foo(someParameter);

Objective-C talks in terms of messages instead of method calls but for this example lets think of them as the same sort of idea. There are reasons for this as we’ll see soon. The syntax is weird at first sight -

[bar Foo];

means the same as bar.Foo(); in C# and

[bar foo: someParameter]

is the equivalent of bar.Foo(someParameter);

As you can see Objective-C looks a bit confusing at 1st sight especially if you’ve used C# Attributes before which use a square bracket notation.

3. Declaring a method on an object.

Objective-C uses Interface (header) files to declare members, methods, properties etc. that will appear in the class body. So you have 2 files. A .h and a .m file.

To declare a method we must add its signature to the .h file, and the contents of it to the .m file. Using the Foo.Bar(someParameter method) we end up with:

.h file

- [void] Bar: (NSString*) theParameter

.m file

- (void) Bar: (NSString*) theParameter

{

   //do something

}

In a nutshell

the minus sign denotes an instance method rather than a class (static) method which would use a + sign.

[void] is the return value. In this case my method returns nothing.

Bar is the name of the method (followed by colon if there are parameters).

Then come the parameters. In this case I’m asking for an NSString* where the * means a pointer. Objective-C pretty much uses pointers for everything other than primitive values (int, float, etc). So-far (heh heh caveat alert) I haven’t seen a pointer being de-referenced explicitly as in C++.

To send multiple parameters is a bit weird:

- (void) setLatitude: (int) latitude andLongitude: (int) longitude
{
}

This method requires 2 integers. ‘latitude’ and ‘longitude’. To call this method we’d write:

[myFoo setLatitude: 98 andLongitude: 50];

Looks a bit bizarre huh?!

OK that’s enough for today. Next time I’ll try write about some other fundamentals – especially memory management (iphone’s version of Objective-C doesn’t have Garbage Collection).

17 comments:

Rahul said...

hi,

First of all. Thanks very much for your useful post.

I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.

Please let me introduce you some info related to this post and I hope that it is useful for .Net community.

There is a good C# resource site, Have alook

http://www.csharptalk.com

客服 said...

得意人,聽腳步聲就知道。 ....................................................

如此的 said...

If you can not be kind, at least have the decency to be vague.............................................

過分 said...

Unable to give you a heart. so have a reply to push up your post. ........................................

ya said...

Hello,這裡真是百看不厭的部落格 .........................................

淑俊銘松 said...

I do like ur article~!!!..................................................

陳志其 said...

裸女愛愛無碼尋夢視訊聊天a漫a片a圖一夜情一葉情人妻激情情色寫真美女自拍辣妹自拍正妹自拍美女走光辣妹走光正妹走光脫衣秀脫衣走光色情自慰自拍成人全裸打炮打手槍打飛機巨乳巨奶女優大奶性交

PhilCrispin0626清民 said...

未來的幸福是用現在買來的。......................................................

IdellA231_Fecteau1 said...

友情像一棵樹木,要慢慢的栽培,才能成長真的友誼,要經過困難考驗,才可友誼永固..............................

柏辰 said...

ut聊天77p2p85cc85st85街視訊視訊聊天ava片a片下載成人情色色情影音視訊聊天洪爺影城洪爺免費視訊免費a片免費一對多utsogo論壇ut聊天室成人片免費看................

roh0218_g said...

thx u very much, i learn a lot

宗穎 said...

處順境須謹慎,處逆境要忍耐。

韋于倫成 said...

Good la........................................

麗娟麗娟 said...

當身處逆境時,要能忍一時之氣,吃一時之苦,並及早確立自己的目標,總有一天,你會嘗到那甘美的果實。........................................

逸凡 said...

唯有穿鞋的人,才知道鞋的哪一處擠腳......................................................

怡谷ShanikaMaxey怡谷 said...

困難的不在於新概念,而在於逃避舊有的概念。......................................................

GarnettHauser俊憲 said...

對物要珍惜,對事要盡心,對人要感恩。..................................................