I'm making a class, that given an object
target, a selector
to watch for, and a displayTitle
will output a string in this format: @"displayTitle: object.selector"
. It then registers itself through KVO so that anytime the value
of object.selector
changes, it can notify a view controller to update the view. I am using this as an abstract and reusable way to show a description of various properties of an object to a user.
When I try to get the value of object.selector
, I can't do [object performSelector:selector]
because LLVM gives errors when you use performSelector with a dynamic selector.
So, I did exactly what this answer suggested: I used objc_msgSend(object, selector)
.
- (instancetype)initWithSelector:(SEL)selector onObject:(NSObject*)object displayTitle:(NSString*)displayTitle {
self = [super init];
if (self) {
id value;
if ([object respondsToSelector:selector) {
// Used objc_msgSend instead of performSelector to suppress a LLVM warning which was caused by using a dynamic selector.
value = objc_msgSend(object, selector);
} else {
return nil;
}
[self setItemDescription:[NSString stringWithFormat:@"%@: %@", displayTitle, value]];
}
return self;
}
And I got an EXC_BAD_ACCESS
!
As you can see in the screenshot, I made sure that
doing [object selector]
works.
What is going on, and how can I fix it?