2

I'm uncertain how to use this method, I've tried code below and assumed that both should return YES, but I'm observing something different. Can somebody please explain? [UILocalNotification setRegion:] is new in iOS8.

BOOL test0 = [[[UILocalNotification alloc] init] respondsToSelector:@selector(setRegion:)];
BOOL test1 = [UILocalNotification instancesRespondToSelector:@selector(setRegion:)];

debug window shows test values to be:

test0 = (BOOL)YES;
test1 = (BOOL)NO;
Mahakala
  • 761
  • 1
  • 10
  • 16
  • possible duplicate of [What is the difference between instancesRespondToSelector and respondsToSelector in Objective-C?](http://stackoverflow.com/questions/11574478/what-is-the-difference-between-instancesrespondtoselector-and-respondstoselector) – Rick Nov 14 '14 at 11:46
  • 1
    you're not `init`-ing after you allocate in the second, which explains the `NO`. If you don't have an instance, it couldn't respond to a selector. – Todd Nov 14 '14 at 11:48
  • instancesRespondToSelector is a class method (signature is: +instancesRespondToSelector:), according to Apple: Returns a Boolean value that indicates whether instances of the receiver are capable of responding to a given selector. – Mahakala Nov 14 '14 at 11:57
  • 3
    @Todd, he's not allocating in the second call, he's using a class method that returns a bool, therefore, he doesn't need to alloc-init. – AMI289 Nov 14 '14 at 12:03
  • See the second answer in the question Rick linked to: [2nd SO Answer](http://stackoverflow.com/a/11574745/451475) by wattson12. – zaph Nov 14 '14 at 12:13
  • lol. My sleep deprivation is starting to show. Upvote. – Todd Nov 14 '14 at 12:15

1 Answers1

3

Whilst understanding that one is a class method, and the other an instance method, I couldn't understand how (or why) the two would return different answers. To add to the confusion, if I do

BOOL test2 = [[[[UILocalNotification alloc] init] class] instancesRespondToSelector:@selector(setRegion:)];

then the answer is YES! So I checked, and if you look at the class for the object returned by alloc init, it is different:

UILocalNotification *local = [[UILocalNotification alloc] init];
NSLog(@"%@", NSStringFromClass([local class]));
NSLog(@"%@", NSStringFromClass([UILocalNotification class]));

returns:

2014-11-14 12:48:14.990 Test[6750:22555] UIConcreteLocalNotification
2014-11-14 12:48:14.991 Test[6750:22555] UILocalNotification

Which explains how come the answers are different.

pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • Good answer, it makes sense now. Although it would be nice if docs mention that UILocalNotification is just an interface. Thanks. – Mahakala Nov 14 '14 at 13:44
  • Agreed regarding the docs. I couldn't see anything in the UILocalNotification.h either. – pbasdf Nov 14 '14 at 13:59