Objective-C: Difference between revisions

From Medien Wiki
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Hello World ==
== Hello World (new ARC enabled example) ==


Xcode, Interface Builder und Co. können sehr viel, aber für den Anfang lassen sie vieles komplizierter erscheinen als es ist. Hier ist ein kurzes Programm in Objective-C.
Xcode, Interface Builder und Co. are very powerful tools, but sometimes they seem to make things a little bit more complicated than they really are. Here's a short program in Objective-C:
 
<source lang="objc">
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
    @autoreleasepool {
   
// Hello World!
printf("About to print the most important message...\n");
NSLog(@"Hello World!\n");
    }
    return 0;
}
</source>
 
You can compile either with a simple Editor that supports this (e.g. CodeRunner or [http://macromates.com/ TextMate]: Actions: Run) - or you use the clang (speak: C-Lang) compiler with Bash (Terminal.app):
<source lang="bash">
cd ~/Path/to/HelloWorld.m
clang HelloWorld.m -o HelloWorld -framework Foundation
./HelloWorld
</source>
 
== Hello World (old, non-ARC example) ==
 
Xcode, Interface Builder und Co. are very powerful tools, but sometimes they seem to make things a little bit more complicated than they really are. Here's a short program in Objective-C:


<source lang="objc">
<source lang="objc">
Line 10: Line 36:
      
      
// Hello World!
// Hello World!
NSLog(@"About to print the most important message...");
printf("About to print the most important message...\n");
printf("Hello World!\n");
NSLog(@"Hello World!\n");
     [pool release];
     [pool release];
Line 18: Line 44:
</source>
</source>


Kompilieren kann man das z.B. mit TextMate (einfach unter Actions &gt; Run wählen) oder im Terminal mit:  
You can compile either with a simple Editor that supports this (e.g. [http://macromates.com/ TextMate]: Actions: Run) - or you use the gcc compiler with Bash (Terminal.app):
<source lang="bash">
<source lang="bash">
cd ~/Path/to/HelloWorld.m
cd ~/Path/to/HelloWorld.m
Line 25: Line 51:
</source>
</source>


 
== Objective-C Introduction ==
== Objective-C ==
* [http://en.wikipedia.org/wiki/Objective-C en.wikipedia.org/wiki/Objective-C] - very good, because short & intrinsic Overview
Eine sehr gute, weil knappe und übersichtliche Seite zu Objective-C findet man auf Wikipedia:
* [http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html Objective-C] - official Apple Docs on Objective-C
* [http://en.wikipedia.org/wiki/Objective-C en.wikipedia.org/wiki/Objective-C]


== Objective-C versus ... ==
== Objective-C versus ... ==
* [http://www.approximity.com/ruby/Comparison_rb_st_m_java.html Comparison Chart] between ObjC, C++, Java, Ruby, Smalltalk, ...
* [http://www.approximity.com/ruby/Comparison_rb_st_m_java.html Comparison Chart] between ObjC, C++, Java, Ruby, Smalltalk, ...
== NeXTStep ==
Cocoa and Objective-C kind of belong to each other. Usually you'll hear that Objective-C is only useful on Mac OS and bound to Apple Stuff only, but this is not true: While NeXTStep originated over 20 years ago, there's also GNUstep which aims to bring Cocoa to all major OS (*nix & Windows):
* [http://www.gnustep.org/ GNUstep.org]
** [http://en.wikipedia.org/wiki/GNUstep GNUstep on Wikipedia]
** [http://en.wikipedia.org/wiki/NeXTStep History of NeXtStep]
Basically GNUstep's Project Center is like Xcode and Gorm is like Interface Builder.
...btw, that's where all the NS-Prefixes come from ('''N'''eXT'''S'''tep), eg in NSObject, NSArray, ...


{{Template:iPhoneDev}}
{{Template:iPhoneDev}}

Latest revision as of 18:53, 23 April 2013

Hello World (new ARC enabled example)

Xcode, Interface Builder und Co. are very powerful tools, but sometimes they seem to make things a little bit more complicated than they really are. Here's a short program in Objective-C:

#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[]) {
    @autoreleasepool {
    
	// Hello World!
	printf("About to print the most important message...\n");
	NSLog(@"Hello World!\n");
	
    }
    return 0;
}

You can compile either with a simple Editor that supports this (e.g. CodeRunner or TextMate: Actions: Run) - or you use the clang (speak: C-Lang) compiler with Bash (Terminal.app):

cd ~/Path/to/HelloWorld.m
clang HelloWorld.m -o HelloWorld -framework Foundation
./HelloWorld

Hello World (old, non-ARC example)

Xcode, Interface Builder und Co. are very powerful tools, but sometimes they seem to make things a little bit more complicated than they really are. Here's a short program in Objective-C:

#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
	// Hello World!
	printf("About to print the most important message...\n");
	NSLog(@"Hello World!\n");
	
    [pool release];
    return 0;
}

You can compile either with a simple Editor that supports this (e.g. TextMate: Actions: Run) - or you use the gcc compiler with Bash (Terminal.app):

cd ~/Path/to/HelloWorld.m
gcc HelloWorld.m -o HelloWorld -framework Foundation
./HelloWorld

Objective-C Introduction

Objective-C versus ...

NeXTStep

Cocoa and Objective-C kind of belong to each other. Usually you'll hear that Objective-C is only useful on Mac OS and bound to Apple Stuff only, but this is not true: While NeXTStep originated over 20 years ago, there's also GNUstep which aims to bring Cocoa to all major OS (*nix & Windows):

Basically GNUstep's Project Center is like Xcode and Gorm is like Interface Builder.

...btw, that's where all the NS-Prefixes come from (NeXTStep), eg in NSObject, NSArray, ...



Diese Seite ist Teil des Werkmoduls iOS Development von Michael Markert für Interface Design / Fakultät Medien an der Bauhaus-Universität Weimar.