Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is down-casting within block parameters a bad practice?

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?

like image 456
Edwin Iskandar Avatar asked Jun 10 '26 05:06

Edwin Iskandar


1 Answers

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.

like image 180
Metabble Avatar answered Jun 17 '26 22:06

Metabble



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!