最近在ubuntu学习objective c,于是找到了下面的办法 ,懒得翻译了,哈哈,这样学习objective c就暂时不用弄虚拟机了。
Objective C is the primary language for developing software for the Apple Mac. In fact, apparently you're only allowed to use that, rather than all the other wonderful programming languages. But that's another story (see
section 3.3.1of the SDK agreement).
Objective Cadds Smalltalk style messaging on top of C. You don't need an Apple Mac to start writing Objective C, on Ubuntu you can just run the following to get all the edpendencies
I think that's the right list - those short of disk space could probably find a more minimal set! If you get crazy errors like this:
Make sure that your shell start up script (.bashrc or .zshrc) has
Hopefully that's a more condensed version of the tortuous path I went through to get things installed... Back to the interesting bits.
The hello worldprogram is exactly the same as for C. By convention ObjectiveC programs use the ".m" extension (for method). You can compile this by simply doing
Obviously that's not very interesting. Header files are declared in a
Implementation files follow a similar pattern,
We can use this object in the main file (
Building this requires a makefile, following the instructions in the tutorialI created
Which when build gives me an executable in
Hurrah, my first ObjectiveC app... Now it's only a small matter of time before I become rich on the app store and can retire! Or not :)
Objective Cadds Smalltalk style messaging on top of C. You don't need an Apple Mac to start writing Objective C, on Ubuntu you can just run the following to get all the edpendencies
sudo apt-get install gobjc gnustep gnustep-make gnustep-common
I think that's the right list - those short of disk space could probably find a more minimal set! If you get crazy errors like this:
/usr/share/GNUstep/Makefiles/GNUstep.sh:288: no such file or directory:
/usr/share/GNUstep/Makefiles/print_unique_pathlist.sh
Make sure that your shell start up script (.bashrc or .zshrc) has
#GNUSTEP Environment vars . /usr/share/GNUstep/Makefiles/GNUstep.sh
Hopefully that's a more condensed version of the tortuous path I went through to get things installed... Back to the interesting bits.
The hello worldprogram is exactly the same as for C. By convention ObjectiveC programs use the ".m" extension (for method). You can compile this by simply doing
gcc helloworld.m
#include <stdio.h>
int main(int n, char** argv) {
printf("Hello world\n");
return 0;
}
Obviously that's not very interesting. Header files are declared in a
.hfile and take the following form.
- An
@interfacedeclaration simply states the data of the object, following that there are a number of method declarations. -
+indicates a class level message (e.g. a static) -
-indicates an instance method. -
NSObjectis the object we inherit from
NSStringis the interface for immutable strings, similar to
std::stringor
java.lang.String. I created
hello.h.
#import <Foundation/Foundation.h>
@interface Hello : NSObject{
NSString* name;
}
+ (NSString*) details;
- (NSString*) name;
- (void) setName : (NSString*)newName;
@end
Implementation files follow a similar pattern,
hello.mlooks like this. retain/release are messages on the
base objectwhich control when an object goes out of scope by using reference counting. I'm sure there's much more to it than that, but I found these
simple rulesexplained what I needed to get going without much hassle.
#include "hello.h"
@implementation Hello
+ (NSString*) details {
return @"This is a string.";
}
- (NSString*) name {
return name;
}
- (void) setName: (NSString*)newName {
[name release];
name = [newName retain];
}
We can use this object in the main file (
helloworld.m). The first line initializes a pool because some of the
NSStringconstructors return autoreleased objects. Sending messages to objects uses the [] syntax. The example below shows a static method call (send a message to the class object itself) and a method call (send the message to the instance).
#include "hello.h"
int main(int n, const char* argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString* s = [Hello details];
printf("Result from a static call: %s\n", [s UTF8String]);
Hello* hello = [[Hello alloc] init];
printf("Name is not initialized: %s\n", [[hello name] UTF8String]);
[hello setName: @"Jeff"];
printf("Your name is %s\n", [[hello name] UTF8String]);
return 0;
}
Building this requires a makefile, following the instructions in the tutorialI created
GNUmakefilewith the following:
include $(GNUSTEP_MAKEFILES)/common.make APP_NAME = Hello Hello_OBJC_FILES = hello.m helloworld.m include $(GNUSTEP_MAKEFILES)/application.make
Which when build gives me an executable in
./Hello.app/Hello:
Result from a static call: This is a string. Name is not initialized: (null) Your name is Jeff
Hurrah, my first ObjectiveC app... Now it's only a small matter of time before I become rich on the app store and can retire! Or not :)
本文介绍了如何在Ubuntu操作系统上安装ObjectiveC环境,并通过简单的hello world程序演示了ObjectiveC的基本语法和类继承机制。重点包括ObjectiveC的类继承、消息传递、字符串操作和方法调用。
1846

被折叠的 条评论
为什么被折叠?



