Skip to content

Instantly share code, notes, and snippets.

@shelling
Created October 25, 2011 02:45
Show Gist options
  • Select an option

  • Save shelling/1311158 to your computer and use it in GitHub Desktop.

Select an option

Save shelling/1311158 to your computer and use it in GitHub Desktop.
use Objective-C in Linux environment
a.out
*.o
*.d
#import <Foundation/Foundation.h>
@interface Animal : NSObject {
@public
NSString *name;
}
-(id)setName: (NSString *)n;
-(id)name;
@end
#import "Animal.h"
@implementation Animal
-(id)setName: (NSString *)n {
name = n;
return self;
}
-(id)name {
return name;
}
@end
#import <Foundation/Foundation.h>
@interface Face : NSObject {
NSString *name;
}
@property (nonatomic, copy) NSString *name;
@end
#import "Face.h"
@implementation Face
@synthesize name;
@end
#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
#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
#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;
}
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