LoginSignup
1
1

More than 5 years have passed since last update.

Fast Enumeration で使う変数のスコープ

Last updated at Posted at 2012-03-16

for in ループにスコープ外の変数を指定して、最後の項目を取得しようとしてもうまくいかない。ループを終えたときには nil になっている。

    NSArray *ary=[NSArray arrayWithObjects:@"a", @"b", @"c", nil];
    id item=@"test start";
    id other=nil;
    for (NSInteger i = 0; i<[ary count]; i++) {
        item=[ary objectAtIndex:i];
    }
    NSLog(@"for = %@", item); // for = c
    item=@"for end";
    for (item in ary) {
        item=item;
        other=item;
    }
    NSLog(@"for in = %@", item); // for in = (null)
    NSLog(@"other = %@", other); // other = c
1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1