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 id arguments. 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 an id and back without casts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With