Objective-C で Algorithm - 0と1を次々と返す簡単なお仕事

initializer と free message の送信、 @private directive を追加。

覚え立ての言語を使いたい。

/*
 * flipflop.m
 *  flipflop class
 *
 *  written by janus_wel<janus.wel.3@gmail.com>
 *  This source code is in public domain, and has NO WARRANTY.
 * */

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

@interface FlipFlop : Object {
@private
    bool state;
}
- (id) init;
- (id) free;

- (bool) state;
@end

@implementation FlipFlop
- (id) init {
    [super init];
    state = true;
    return self;
}
- (id) free {
    return [super free];
}

- (bool) state {
    state = !state;
    return state;
}
@end

int main(void) {
    FlipFlop* ff = [FlipFlop new];
    printf("%d\n", [ff state]);
    printf("%d\n", [ff state]);
    printf("%d\n", [ff state]);
    printf("%d\n", [ff state]);
    [ff free];

    return 0;
}