Not documented: NSArray responds to -allObjects
Not documented: NSArray responds to -allObjects
- Subject: Not documented: NSArray responds to -allObjects
- From: Jerry Krinock <email@hidden>
- Date: Thu, 24 Oct 2013 07:19:51 -0700
Starting somewhere after OS X 10.6, NSArray instances respond to the NSSet method -allObjects. I can’t find any documentation of this. Also, it is not declared in the header NSArray.h
Although it does what you’d expect, returning a copy of self, this can lead to some interesting bugs, particularly when running in 10.6 after testing in a current system :( And in some cases one may want to reconsider the usual strategy of preferring -respondsToSelector: instead of -isKindOfClass: for introspection.
Just to make sure that I hadn’t implemented -allObjects in a forgotten category, I ran the following test in a new little Command-Line Tool project and target…
#import <Foundation/Foundation.h>
void Check(Class class) {
BOOL doesRespond = [class instancesRespondToSelector:@selector(allObjects)] ;
NSLog(@"%hhd for %@", doesRespond, NSStringFromClass(class)) ;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
Check([NSSet class]) ;
Check([NSMutableSet class]) ;
Check([NSArray class]) ;
Check([NSMutableArray class]) ;
Check([NSString class]) ;
Check([NSMutableString class]) ;
NSObject* a = [NSArray arrayWithObjects:
@"a", @"b", @"c", nil] ;
SEL selector = @selector(allObjects) ;
if ([a respondsToSelector:selector]) {
NSLog(@"%@ %p DOES respond to allObjects.", [a className], a) ;
NSSet* s = [a performSelector:selector] ;
NSLog(@"allObjects returned %@ %p: %@", [s className], s, s) ;
}
else {
NSLog(@"%@ does NOT respond to allObjects.", [a className]) ;
}
}
return 0 ;
}
*** Result, in 10.9:
2013-10-24 07:02:19.752 Junk[27092:303] 1 for NSSet
2013-10-24 07:02:19.755 Junk[27092:303] 1 for NSMutableSet
2013-10-24 07:02:19.756 Junk[27092:303] 1 for NSArray
2013-10-24 07:02:19.756 Junk[27092:303] 1 for NSMutableArray
2013-10-24 07:02:19.757 Junk[27092:303] 0 for NSString
2013-10-24 07:02:19.757 Junk[27092:303] 0 for NSMutableString
2013-10-24 07:02:19.758 Junk[27092:303] __NSArrayI 0x100400d10 DOES respond to allObjects.
2013-10-24 07:02:19.759 Junk[27092:303] allObjects returned __NSArrayI 0x100300d90: (
a,
b,
c
)
Program ended with exit code: 0
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden