As an example, instead of writing this:
NSArray *someArray = @[@"1", @"2", @"3", @"4"];
[someArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *aString = obj;
// do something
}];
You can down-cast the object directly if you know the constants in the block method to make it more consice:
[someArray enumerateObjectsUsingBlock:^(NSString *aString, NSUInteger idx, BOOL *stop) {
// do something
}];
Does this go against any best practices or oop principles?
I’m pretty sure that’s fine. As long as you know what’s in the array, feel free to statically type the
idarguments. It’s mostly syntactic sugar at the end of the day anyways. I always statically type anything I can. It helps me catch bugs as well as makes things easier to read. Also, as H2CO3 pointed out, objects can be assigned to anidand back without casts.