I would have thought that this was an identity, but it doesn't seem to be working for me right now.
Behold my lldb prompt:
po [self class]
==> MyCustomClass
po [MyCustomClass class]
==> MyCustomClass
po [self class] == [MyCustomClass class]
==> true
p [self isKindOfClass:[MyCustomClass class]]
==> '\0' (i.e. false).
Definition of isKindOfClass: "Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class."
What kind of nonsense (my fault or otherwise) could cause this kind of behavior?
Background for those who care: I'm unfortunately working with a 3rd-party API that needs me to pass in an object as a void pointer, then to cast it back to the proper type. Despite the fact that everything is compiling just fine, at runtime I'm being told the the object doesn't recognize the selectors I'm trying to send to it. This means (I think?) that it's not actually an instance of the class that I'm casting it to. In attempting to test it, I come across this issue with [NSObject isKindOfClass]
. What on earth is going on here?
Running on an iOS 8.1 iPad, building in XCode 6.1.1.
Here's a screenshot of the last test:
PROGRESS EDIT: I finally managed to get a breakpoint into the thread this library is running on using this post, and am seeing some even crazier stuff.
given (void*)data
, I get:
po [data class]
==> MyCustomClass
po [data isKindOfClass:[MyCustomClass class]]
==> nil
(again, this just means false)
Obviously I've really broken something here. Are there any known issues that could cause something like this?
EDIT SSCCE-ish example: You start with a naked C function that gets passed to a background library as a callback function:
void libraryCallbackFunction(eventname, stuff, (void*)data)
The library calls this function (from a new thread) when an event occurs, and you use this function to handle the event. data
is supposed to be a pointer to any old class that you want to call out to from this event handler function. Inside this function, you cast data to whatever class you want. Then, you can send messages to the data instance on the main thread. Let's call it a view controller:
void libraryCallbackFunction((void*)data) {
MyCustomClass *myViewController = (__bridge MyCustomClass*)data;
/* stuff stuff stuff */
dispatch_async(dispatch_get_main_queue(), ^{
[myViewController makeInterfaceChange];
});
}
This snippet compiles just fine for me, but at runtime I'm getting an error:
NSInvalidArgumentException', reason: '+[MyCustomClass makeInterfaceChange]: unrecognized selector sent to class 0xf3fad8
So in other words, it appears that data
doesn't actually point to an instance of MyCustomClass. Yes, makeInterfaceChange is defined for MyCustomClass, otherwise it wouldn't compile. As I've mentioned before, calling po [data class]
from lldb when at a breakpoint in this function gives MyCustomClass
.
Any ideas?
WAIT, COULD IT BE?: The critical (__bridge) cast in this part of the code isn't syntax highlighting, even though it is everywhere else in my project. Issue? Or just XCode being finicky with syntax highlighting, as usual? Is there any bonehead mistake that might cause __bridge to not work?