Is it allowed to "modify" / abuse / override the block signature in Objective-C as follows?

Is this allowed and why?

void (^bar)(NSNumber *) = ^(NSNumber *number) {
    NSLog(@"Value is %@, class is %@.", number, [number class]);
};
bar([NSNumber numberWithInt:10]);

void (^foo)(id) = bar;
foo([NSDate date]);

Output:

Value is 10, class is __NSCFNumber.
Value is 2012-05-17 18:54:14 +0000, class is __NSDate.

I could not find anything that explains this. Can you provide a link to a specification of object c-blocks that covers this?

I am currently working on a block subclass of UITableView and it will make it easier if I'm safe to use this.

+5
source share
1 answer

The underlying reasons for this is the combination of:

[1] Objective-C ( C) . , ( ) . , , , , NSNumber *, , , , id. , . .

[2] Objective-C , . , number NSNumber NSDate . bar :

void (^bar)(NSNumber *) = ^(NSNumber *number)
{
   NSLog(@"Value is %@, class is %@, int value is %d.", number, [number class], [number intValue]);
};

.

[3] [NSNumber numberWithInt:10], [NSDate date], id, NSNumber * NSDate *, . , foo, :

bar([NSDate date]);

- ... :

NSNumber *num = [NSNumber numberWithInt:3];
NSDate *date = num;         // produces a warning
id erase = num;             // erase type info and do...
date = erase;               // effectively the same assignment, no warning

: Objective-C , , .

+6

All Articles