Objective-C 入門

WisdomSoft『Objective-C 入門』 の 勉強メモ

#import <stdio.h>
#import <objc/Object.h>

int main(int argc,char *argv[])
{
    printf("Hello World.\n");
    return 0;
}
#import <stdio.h>
#import <objc/Object.h>

int main() {
    printf("ザ・ワールド 時よ止まれッ!\n");
    printf("WRYYYYYYYYYYYYーーーーッ\n");

    return 0;
}
#import <stdio.h>
#import <objc/Object.h>

@interface Test : Object
- (void)method;
@end

@implementation Test
- (void)method {
    printf("Kitty on your lap\n");
}
@end

int main() {
    id obj = [Test alloc];
    [obj method];
    
    return 0;
}
#import <stdio.h>
#import <objc/Object.h>

@interface Point : Object
{
    int x, y;
}
- (void)setLeft:  (int)ptx andTop:(int)pty;
- (void)setPoint1:(int)ptx:(int)pty;
- (void)setPoint: (int)ptx int:(int)pty;
- (int)getX;
- (int)getY;
@end

@implementation Point
- (void)setLeft:(int)ptx andTop:(int)pty {
    x = ptx;
    y = pty;
}
- (void)setPoint1:(int)ptx :(int)pty {
    self->x = ptx;
    self->y = pty;
}
- (void)setPoint:(int)ptx int:(int)pty {
    x = ptx;
    y = pty;
}
- (int)getX {
    return self->x;
}
- (int)getY {
    return y;
}
@end

int main() {
    id point1 , point2 , point3;
    point1 = [Point alloc];
    point2 = [Point alloc];
    point3 = [Point alloc];

    [point1 setLeft:1    andTop:4];
    [point2 setPoint:2   int:5];
    [point3 setPoint1:3 :6];

    printf("point1:X=%d, Y=%d\n", [point1 getX] , [point1 getY]);
    printf("point2:X=%d, Y=%d\n", [point2 getX] , [point2 getY]);
    printf("point3:X=%d, Y=%d\n", [point3 getX] , [point3 getY]);

    [point1 free];
    [point2 free];
    [point3 free];

    return 0;
}
#import <stdio.h>
#import <objc/Object.h>

@interface SuperClass : Object
- (void)methodA;
@end

@interface SubClass : SuperClass
-(void)methodB;
@end

@implementation SuperClass
- (void)methodA {
    printf("SuperClass.methodA\n");
}
@end

@implementation SubClass
- (void)methodB {
    printf("SubClass.methodB\n");
}
@end

int main() {
    id obj = [SubClass alloc];

    [obj methodA];
    [obj methodB];
    
    [obj free];
    [obj free];

    return 0;
}#import <stdio.h>
#import <objc/Object.h>

@interface SuperClass : Object
{
    int x;
}
- (void)methodA;
- (void)methodB;
@end

@interface SubClass : SuperClass
-(void)methodA;
-(void)methodB;
@end

@implementation SuperClass
- (void)methodA {
    printf("SuperClass.methodA\n");
}
- (void)methodB {
    printf("SuperClass.methodB\n");
}
@end

@implementation SubClass
- (void)methodA {
    printf("SubClass.methodA %d\n" , x);
}
- (void)methodB {
    printf("SubClass.methodB %d\n" , x);
    [super methodB];
}
@end

int main() {
    [[SubClass alloc] methodA];
    [[SubClass alloc] methodB];
    return 0;
}#import <stdio.h>
#import <objc/Object.h>

@interface Point : Object
{
    int x, y;
}
- (id)init;
- (id)initWithLeft:(int)x andTop:(int)y;
- (int)getX;
- (int)getY;
@end

@implementation Point
- (id)init {
    [super init];

    x = y = 3;
    printf("init method\n");
    return self;
}
- (id)initWithLeft:(int)x andTop:(int)y {
    self->x = x;
    self->y = y;
    printf("initWithLeft andTop method\n");
    return self;
}
- (id)free {
    printf("free method\n");
    return [super free];
}
- (int)getX { return x; }
- (int)getY { return y; }
@end

int main() {
    id pt1 = [Point alloc];
//  id pt2 = [Point init];
    id pt2 = [[Point alloc] init];
    id pt3 = [Point new];
    id pt4 = [[Point alloc] initWithLeft:400 andTop:500];

    printf("X=%d, Y=%d\n" , [pt1 getX] , [pt1 getY]);
    printf("X=%d, Y=%d\n" , [pt2 getX] , [pt2 getY]);
    printf("X=%d, Y=%d\n" , [pt3 getX] , [pt3 getY]);
    printf("X=%d, Y=%d\n" , [pt4 getX] , [pt4 getY]);

    [pt1 free];
    [pt2 free];
    [pt3 free];
    [pt4 free];

    return 0;
}#import <stdio.h>
#import <objc/Object.h>

@interface A : Object
- (void)Write;

@end


@implementation A
- (void)Write {
    printf("A . Write Method\n");
}
@end

int main() {
    A* objA = [A alloc];
    [objA Write];
    [objA free];

    A* objB = [A new];
    [objB Write];
    [objB free];

    return 0;
}#import <stdio.h>
#import <objc/Object.h>

@interface A : Object
{
@public
    int a;
@protected
    int b;
@private
    int c;
}
- (id)initWithA:(int)a int:(int)b int:(int)c;
- (void)WriteA;
@end

@interface B : A
- (void)WriteB;
@end

@implementation A
- (id)initWithA:(int)a int:(int)b int:(int)c {
    self->a = a;
    self->b = b;
    self->c = c;
    return self;
}
- (void)WriteA {
    printf("[A Write Method, a=%d, b=%d, c=%d]\n", a , b , c);
}
@end

@implementation B
- (void)WriteB {
    printf("[B Write Method, a=%d, b=%d]\n", a , b);
}
@end

int main() {
    B * objb = [[B new] initWithA:1000 int:100 int:10];
    printf("[main() scope, a=%d]\n" , objb->a);
    [objb WriteB];
    [objb WriteA];
    [objb free];
    return 0;
}
#import <stdio.h>
#import <objc/Object.h>

@interface Test : Object
+ (void)Write;
@end

@implementation Test
+ (void)Write {
    printf("I love you... so please do not love me.\n");
}
@end

int main() {
    [Test Write];
    return 0;
}
#import <stdio.h>
#import <objc/Object.h>

@interface Test : Object
+ (void)Write;
- (id)init;
@end

@implementation Test
+ (void)Write {
    printf("I love you... so please do not love me.\n");
}
- (id)init {
    printf("You can be whatever.\n");
    return [super init];
}
@end

int main() {
    Class testClass = [Test class];
    [testClass Write];
    [testClass free];

    Test* objTest = [Test new];
    [objTest Write];
    [objTest free];

    return 0;
}
#import <stdio.h>
#import <objc/Object.h>

@interface Test : Object
- (void)Write;
@end

@implementation Test
- (void)Write {
    printf("I am the bone of my sword.\n");
}
@end

int main() {
    id obj = [Test new];
    SEL method = @selector(Write);
    [obj perform:method];
    [obj free];

    return 0;
}#import <stdio.h>
#import <objc/Object.h>

@interface Test : Object
- (void)Write;
@end

@implementation Test
- (void)Write {
    printf("I am the bone of my sword.\n");
}
@end

int main() {
    id  obj    = [Test new];
    SEL method = @selector(Write);
    IMP func   = [Test instanceMethodFor:method];

    func(obj , method);

    [obj free];
    return 0;
}
#import <stdio.h>
#import <objc/Object.h>

@protocol ClassNameToString
- (id) ToString;
@end

@interface A : Object <ClassNameToString>
@end

@interface B : Object <ClassNameToString>
@end

@implementation A
- (id) ToString { return (id)"This is Object of A Class"; }
@end
@implementation B
- (id) ToString { return (id)"This is Object of B Class"; }
@end

int main() {
    id <ClassNameToString> objA = [A new];
    id objB = [B new];
    printf("objA = %s\n" , [objA ToString]);
    printf("objB = %s\n" , [objB ToString]);
    [objA free];
    [objB free];

    return 0;
}
xxxxx@yyyyy ~
$ $ gcc lesson001.m -o lesson001 -lobjc
bash: $: command not found

xxxxx@yyyyy ~
$ gcc lesson001.m -o lesson001 -lobjc

xxxxx@yyyyy ~
$ ./lesson001.exe
ザ・ワールド 時よ止まれッ!
WRYYYYYYYYYYYYーーーーッ

xxxxx@yyyyy ~
$ gcc lesson002.m -o lesson002 -lobjc

xxxxx@yyyyy ~
$ ./lesson002.exe
Kitty on your lap

xxxxx@yyyyy ~
$ gcc lesson003.m -o lesson003 -lobjc

xxxxx@yyyyy ~
$ ./lesson003.exe
point1:X=1, Y=4
point2:X=2, Y=5
point3:X=3, Y=6

xxxxx@yyyyy ~
$ gcc lesson004.m -o lesson004 -lobjc

xxxxx@yyyyy ~
$ ./lesson004.exe
SuperClass.methodA
SubClass.methodB

xxxxx@yyyyy ~
$ gcc lesson005.m -o lesson005 -lobjc

xxxxx@yyyyy ~
$ ./lesson005.exe
SubClass.methodA 0
SubClass.methodB 0
SuperClass.methodB

xxxxx@yyyyy ~
$ gcc lesson006.m -o lesson006 -lobjc
lesson006.m: In function '-[Point initWithLeft:andTop:]':
lesson006.m:23: 警告: local declaration of 'x' hides instance variable
lesson006.m:24: 警告: local declaration of 'y' hides instance variable

xxxxx@yyyyy ~
$ ./lesson006.exe
init method
init method
initWithLeft andTop method
X=0, Y=0
X=3, Y=3
X=3, Y=3
X=400, Y=500
free method
free method
free method
free method

xxxxx@yyyyy ~
$ gcc lesson007.m -o lesson007 -lobjc

xxxxx@yyyyy ~
$ ./lesson007.exe
A . Write Method
A . Write Method

xxxxx@yyyyy ~
$ gcc lesson008.m -o lesson008 -lobjc
lesson008.m: In function '-[A initWithA:int:int:]':
lesson008.m:23: 警告: local declaration of 'a' hides instance variable
lesson008.m:24: 警告: local declaration of 'b' hides instance variable
lesson008.m:25: 警告: local declaration of 'c' hides instance variable

xxxxx@yyyyy ~
$ ./lesson008.exe
[main() scope, a=1000]
[B Write Method, a=1000, b=100]
[A Write Method, a=1000, b=100, c=10]

xxxxx@yyyyy ~
$ gcc lesson009.m -o lesson009 -lobjc

xxxxx@yyyyy ~
$ ./lesson009.exe
I love you... so please do not love me.

xxxxx@yyyyy ~
$ gcc lesson010.m -o lesson010 -lobjc
lesson010.m: In function 'main':
lesson010.m:25: 警告: 'Test' may not respond to '-Write'
lesson010.m:25: 警告: (Messages without a matching method signature
lesson010.m:25: 警告: will be assumed to return 'id' and accept
lesson010.m:25: 警告: '...' as arguments.)

xxxxx@yyyyy ~
$ ./lesson010.exe
I love you... so please do not love me.
You can be whatever.
error: Test (instance)
Test does not recognize Write
Aborted (コアダンプ)

xxxxx@yyyyy ~
$ gcc lesson011.m -o lesson011 -lobjc

xxxxx@yyyyy ~
$ ./lesson011.exe
I am the bone of my sword.

xxxxx@yyyyy ~
$ gcc lesson012.m -o lesson012 -lobjc

xxxxx@yyyyy ~
$ ./lesson012.exe
I am the bone of my sword.

xxxxx@yyyyy ~
$ gcc lesson013.m -o lesson013 -lobjc
lesson013.m: In function 'main':
lesson013.m:26: 警告: '-free' not found in protocol(s)

xxxxx@yyyyy ~
$ ./lesson013.exe
objA = This is Object of A Class
objB = This is Object of B Class

xxxxx@yyyyy ~
$