I am working on this piece of code, basically adding a block to NSObject:
class_addMethod(object_getClass([NSObject class]), @selector(toUpper2:), imp_implementationWithBlock(^NSString*(id self, SEL _cmd, NSString* s) {
NSLog(@"self: %@", self);
NSLog(@"_cmd: %@", _cmd); // I know %@ is not SEL, but look for yourself
NSLog(@"s: %@", s);
return [s uppercaseString];
}), "@@:@"); // the type signature was created using @encode
To me, this looks fairly innocent, but if I do this: (I've defined a +toUpper2 in a different class too, so that the compiler does not complain):
[(id)[NSObject class] toUpper2:@"hallo"];
This happens:
2018-07-06 16:45:52.302676+0200 xctest[43736:32056962] self: NSObject
2018-07-06 16:45:52.302706+0200 xctest[43736:32056962] _cmd: hallo
2018-07-06 16:45:52.302721+0200 xctest[43736:32056962] s: (null)
As you can see, the arguments got messed up. What's more, if i enact the same method using performSelector, like that:
[NSObject performSelector:@selector(toUpper2:) withObject:@"hallo"];
Then, things get even more astray:
2018-07-06 16:45:52.302737+0200 xctest[43736:32056962] self: NSObject
2018-07-06 16:45:52.302751+0200 xctest[43736:32056962] _cmd: hallo
2018-07-06 16:45:52.302763+0200 xctest[43736:32056962] s: hallo
Can anyone explain this behaviour?
Best regards, thejack