It’s been a short time since I’ve gotten anything written, this was due to work being busy, a few small side projects, school projects and exams, summer starting and well - a new love. This love however can be bought with money and it involves selling a bit of your soul.
After waiting a year I was finally able to get a iPhone. 3Gs at that! This quickly pulled my interest away from my every day languages and set me on a course to learn Objective-C.
Back to the start
Through a tutorial a while back I was able to build a ‘helloworld’ application that ran! It was very exciting and it brought me back to why I enjoy programming so much. However coming from a PHP and Java background (very little C-anything) it was pretty deep water at times. There were odd function calls, and the instance variables were hard identify. It was clear that all elements of an OOP language were there, just at the cost of an odd syntax layout.
Not to get overwhelmed by the complexity of the language I went out and picked up Programming in Objective-C 2.0 (2nd Edition). I’m only on Chapter 3 but unknown tidbits are starting to make a little more sense, and I am now able to identify the common elements between Objective-C and other OOP languages.
The Code
This article isn’t a whole lot on the learning part, just a quick demo on a helloworld application written in PHP, Java and, Objective-C
PHP
<?php
$word1 = 'hello';
$word2 = 'world';
function $printText() {
return $word1.' '.$word2
}
echo $printText();
?>
Java
package ocTest;
public class HelloWorld {
private String word1;
private String word2;
/**
* @param args
*/
public static void main(String[] args) {
new HelloWorld();
}
private HelloWorld() {
setWord1("hello");
setWord2("world");
System.out.println(printText());
}
/**
* @param word1 the word1 to set
*/
public void setWord1(String word1) {
this.word1 = word1;
}
/**
* @param word2 the word2 to set
*/
public void setWord2(String word2) {
this.word2 = word2;
}
private String printText() {
return word1 + " " + word2;
}
}
Objective-C
#import <Foundation/Foundation.h>
// --- @interface ----
@interface TheString: NSObject
{
char word1;
char word2;
}
-(void) printText;
-(void) setWord1: (char) w1;
-(void) setWord2: (char) w2;
@end
// --- @implementation ---
@implementation TheString
-(void) printText
{
NSLog(@"%@ %@", word1, word2);
}
-(void) setWord1: (char) w1
{
word1 = w1;
}
-(void) setWord2: (char) w2
{
word2 = w2;
}
@end
// --- Main ---
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
TheString *myString;
[myString setWord1: 'hello'];
[myString setWord2: 'world'];
[myString printText];
[myString release];
[pool drain];
return 0;
}
Conclusion
I find it quite interesting that such a simple application such as ‘hello world’ can become so complex. Of course I went a little overboard with the instance variables and the setters. More to come soon, hopefully something on the iPhone soon!
Write a Comment