Created
October 25, 2011 02:45
-
-
Save shelling/1311158 to your computer and use it in GitHub Desktop.
use Objective-C in Linux environment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| a.out | |
| *.o | |
| *.d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| @interface Animal : NSObject { | |
| @public | |
| NSString *name; | |
| } | |
| -(id)setName: (NSString *)n; | |
| -(id)name; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "Animal.h" | |
| @implementation Animal | |
| -(id)setName: (NSString *)n { | |
| name = n; | |
| return self; | |
| } | |
| -(id)name { | |
| return name; | |
| } | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| @interface Face : NSObject { | |
| NSString *name; | |
| } | |
| @property (nonatomic, copy) NSString *name; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "Face.h" | |
| @implementation Face | |
| @synthesize name; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| @interface Hello : NSObject { | |
| @public | |
| int numerator; | |
| int denominator; | |
| NSString *string; | |
| } | |
| -(Hello*)initWithString: (NSString*)str; | |
| -(void)hello ; | |
| -(id)echo: (id)str; | |
| -(id)setNumerator: (int)n; | |
| -(id)setDenominator: (int)d; | |
| -(id)setString: (NSString*)str; | |
| -(id)string; | |
| @end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "Hello.h" | |
| @implementation Hello : NSObject | |
| -(Hello*)initWithString: (NSString*)str { | |
| self = [super init]; | |
| [self setString:str]; | |
| return self; | |
| } | |
| -(void)hello { | |
| NSLog(@"hello"); | |
| } | |
| -(id)echo: (id)str { | |
| return str; | |
| } | |
| -(id)setNumerator: (int)n { | |
| numerator = n; | |
| return self; | |
| } | |
| -(id)setDenominator: (int)d { | |
| denominator = d; | |
| return self; | |
| } | |
| -(id)setString: (NSString*)str { | |
| [str retain]; | |
| [string release]; | |
| string = str; | |
| return self; | |
| } | |
| -(id)string { | |
| return string; | |
| } | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| #import "Animal.h" | |
| #import "Hello.h" | |
| #import "Face.h" | |
| @protocol Fly | |
| -(NSString*) fly; | |
| @end | |
| @interface Bird : NSObject <Fly> { | |
| NSString *name; | |
| } | |
| -(NSString*) name; | |
| -(id) setName: (NSString*)n; | |
| @end | |
| @implementation Bird | |
| -(NSString*) fly { | |
| return @"is a bird can fly (protocol)"; | |
| } | |
| -(NSString*) name { | |
| return name; | |
| } | |
| -(id) setName: (NSString*)n { | |
| [n retain]; | |
| [name release]; | |
| name = n; | |
| return self; | |
| } | |
| @end | |
| @interface Hello (Say) | |
| -(NSString*) say; | |
| @end | |
| @implementation Hello (Say) | |
| -(NSString*) say { | |
| return @"hello, i am a hello object"; | |
| } | |
| @end | |
| int main(int argc, char *argv[]) { | |
| NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
| Bird *bird = [[Bird alloc] init]; | |
| NSLog(@"%@", [bird fly]); | |
| [bird setValue:@"lucky" forKey:@"name"]; | |
| [bird setName:@"jennifer"]; | |
| NSLog(@"set name of bird as %@", [bird name]); | |
| // NSString *str = [[NSString alloc] init]; | |
| // NSString *str2 = [NSString new]; | |
| Hello *hello = [[Hello new] autorelease]; | |
| [hello hello]; | |
| NSLog(@"%@", [hello echo:@"echo here"]); | |
| NSLog(@"%@", [hello say]); | |
| Hello *hero = [[[Hello alloc] initWithString:@"set set string"] autorelease]; | |
| NSLog(@"%@", [hero string]); | |
| NSString *init_with_class = [NSString stringWithFormat:@"init with class method here"]; | |
| NSLog(@"%@", init_with_class); // this string is an autorelease one | |
| [[hello setNumerator:1] setDenominator:3]; | |
| Animal *a = [[Animal new] autorelease]; | |
| [a setName:@"lucky"]; | |
| NSLog(@"%@", [a name]); | |
| NSNumber *number = [NSNumber numberWithInteger:100]; | |
| NSLog(@"%@", number); | |
| NSMutableDictionary *hash = [NSMutableDictionary dictionary]; | |
| [hash setObject:@"bar" forKey:@"foo"]; | |
| NSLog(@"%@", hash); | |
| Face *face = [[Face new] autorelease]; | |
| face.name = @"face two"; | |
| NSLog(@"%@", face.name); | |
| [pool drain]; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FLAG=`gnustep-config --objc-flags` | |
| .PHONY: all clean | |
| all: Animal.o Hello.o Face.o | |
| gcc $(FLAG) -lgnustep-base main.m *.o | |
| clean: | |
| rm a.out *.o *.d | |
| Animal.o: Animal.m | |
| gcc -c Animal.m $(FLAG) | |
| Hello.o: Hello.m | |
| gcc -c Hello.m $(FLAG) | |
| Face.o: Face.m | |
| gcc -c Face.m $(FLAG) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment