Check Objective-C generics type in for loop (fast enumeration)

109 Views Asked by At

Is there any compiler option to warn that the type in the for-in loop is wrong?

    NSArray<NSString *> *stringsArray = @[ @"Hello", @"World" ];
    for (UIView *wrongType in stringsArray) {
        NSLog(@"object: %@", wrongType);
    }
1

There are 1 best solutions below

2
Sviatoslav On

Since NSArray may contain objects of multiple classes, there are no way for compiler to detect wrong class in such for loop.

Source - iOS Programming: The Big Nerd Ranch Guide