iPhone programming part 1
When I started developing iPhone applications a mayor problem was to understand some technical terms like what the hell is XCode, Cocoa or Objective C. So here first some very short descriptions the the most wanted terms in iPhone programming:
XCode : Like Visual Studio for Microsoft Windows is XCode a programming plattform to code applications for Apple Mac OS Systems – keep in mind that iPhones are running Mac OS, too.
Cocoa : some may think it’s a pendant to java ( like java was coffee, so cocoa have to be something like coffee for children: cocoa) – but its not! It’s a huge coding library (API) to code your mac applictions. Cocoa classes are starting with “NS”-Tag, like NSString, or NSObject.
Objective C : Objective C, or short ObjC is a programming language extended from standard C. So all standard C code is leagal Objective C code. If you already read something about objective C you probably ask your self what the heck is that thing about these brackets “[ ]” ? Here is a simple code snippet which we will discuss later:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | /////////////////////////////////////////// // FILE : classA.h // Header File for classA /////////////////////////////////////////// #import <uikit /UIKit.h> @interface classA : NSObject { bool myBoolean; int myNumber; int myIdentifier; NSString * name; } // manual creating getters and setters for a property -(int) myNumber; -(void) setMyNumber:(int)value; // automatic creating getters and setters for a property @property bool myBoolean; @property (readonly) int myIdentifier; @property (assign) NSString * name; // methods and functions -(void) doStuffWithA:(bool)a B:(int)b C:(NSString *)c; @end /////////////////////////////////////////// // FILE : classA.m // Implementation File for classA /////////////////////////////////////////// @implementation classA // automatic generation of getters and setters for properties: @synthesize myBoolean; @synthesize myIdentifier; @synthesize name; // manual getter and setter methods: -(void) setMyNumber:(int)value { myNumber = value; } -(int) myNumber { return myNumber; // this is ok. It will return the value. To call the function in ObjC // you have to write something like:"return [self myNumber];" } -(void) doStuffWithA:(bool)a B:(int)b C:(NSString *)c { myBoolean = a; myNumber = b; myName = c; myIdentifier = b; // ok, internal writeable - only external readonly // not allowed: self.myNumber = b; // dot notations are only allowed for automatic generated properties. } @end /////////////////////////////////////////// // FILE main.m // Main Application File /////////////////////////////////////////// #import </uikit><uikit /UIKit.h> #import "classA.h" int main(int argc, char *argv[]) { classA * myObject = [[classA alloc]init]; [myObject setMyNumber:1]; // ok int a = [myObject myNumber]; // ok // myObject.setNumber(1); // not allowed. // int a = myObject.myNumber; // only on automatic generated properties. [myObject setMyBoolean:true]; // ok myObject.myBoolean = true; // ok bool b = [myObject myBoolean]; // ok b = myObject.myBoolean; // ok // [myObject setMyIdentifier:1]; // not allowed : readonly // myObject.myIdentifier = 1; // not allowed : readonly int i = [myObject myIdentifier]; // ok i = myObject.myIdentifier; // ok [myObject doStuffWithA:true B:42 C:@"Text"]; return retVal; } </uikit> |
Some words about this “[ ]” – stuff. First of all, in Objective C you do not call a member function of an object directly (like “object.methodName(value1, value2)”. You retain a controller to sent a message to the object by using these brackets ( [object methodName:value1 param2Name: value2] ).
The basic form is [object param1Name:value1 param2Name:value2 param3Name:value3] where param1Name is the method name itself. That’s why we called our “doStuff” method in classA : “doStuffWithA”. When you call that method it would be read like [doStuffWithA:true B:42 C:@"String"].
Now some may think that Objective C is a bit ugly. You are right – it’s ugly. But never the less it is a powerful toy, so it’s worth to get throu it.
About this entry
You’re currently reading “ iPhone programming part 1 ,” an entry on coders
- Author:
- hhamm
- Published:
- 1.27.09 / 12pm
- Category:
- objective c, programming, xcode
- Feedback:


3 Comments
Jump to comment form | comments rss [?] | trackback uri [?]