Help us understand the problem. What is going on with this article?

Objective C のiteratorパターンを考えてみる

More than 5 years have passed since last update.

概要

最近wantedlyでは、新人研修でjavaのデザインパターンを勉強してます。
ただ使っている言語は、objective-Cだったり、rubyだったりするので、自分の勉強も含めて、それぞれのパターンをそれぞれのどう実装していくか整理しながら進めてます。

iteratorパターン

forで数を数える場合、最後の数を間違えたりしてバグを生み出すことがあります。
またenumratorを利用することで、数を数えることとコレクションの実装を分けて考えることができます。

objective-Cでiteratorを利用する方法

NSFastEnumeration protocolを利用します。NSArrayやNSSetやNSDictionaryは、このprotocolを含んでます。

enumratorの使い分け

  • for inで利用するのが基本です
for (id object in array) {
    NSLog(@"%@", object);
}
  • whileで書く場合
id object = nil;
NSEnumerator *enumerator = ...;
while ((object = [enumerator nextObject])) {
    NSLog(@"%@", object);
}
  • NSArrayやNSSetやNSDictionaryやNSIndexSetではblockでenumerateを使うことができます。
[array enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
    NSLog(@"%@", object);
}];

NSFastEnumrationのプロトコルの使い方

  • NSFastEnumerationのプロトコルは、countByEnumeratingWithStateというmethodが必要になります
BookShelf.h
#import <Foundation/Foundation.h>

#define ARRAY_SIZE 32

@interface BookShelf : NSObject <NSFastEnumeration>
{
    __unsafe_unretained id  _objects[ARRAY_SIZE];
}

@end
BookShelf.m
#import "BookShelf.h"

@implementation BookShelf

- (id)init
{
    self = [super init];
    if (!self) {
        return nil;
    }

    for (int i = 0; i < ARRAY_SIZE; i++) {
        _objects[i] = [NSNumber numberWithInt:i];
    }

    return self;
}

- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len
{
    if (state->state >= ARRAY_SIZE) {
        return 0;
    }

    state->itemsPtr = _objects;
    state->state = ARRAY_SIZE;
    state->mutationsPtr = &state->extra[0];

    return ARRAY_SIZE;
}

@end
main.m
#import <Foundation/Foundation.h>
#import "BookShelf.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        BookShelf *bookShelf = [BookShelf new];

        for (id object in bookShelf) {
            NSLog(@"%@", object);
        }
    }
    return 0;
}

参考

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away