Re: Not documented: NSArray responds to -allObjects
Re: Not documented: NSArray responds to -allObjects
- Subject: Re: Not documented: NSArray responds to -allObjects
- From: Roland King <email@hidden>
- Date: Thu, 24 Oct 2013 22:56:12 +0800
I agree, that method exists, not quite sure what your question is though.
Depends what you want to do with introspection. If you want to ensure a class responds to allObjects and you expect if it does it returns an NSArray, that still works in this case, if you are trying to work out what an object is by checking what methods it supports, it's fragile and is going to get broken however hard you try and that is an unfortunate fact. If it's all your code you can limit that, if you're writing a library you are at the mercy of any category anyone added to anything anywhere.
I do personally feel that somewhere around the time class clusters arrived on the scene, a method isKindOfClassReally should have arrived too which hides the implementation from the interface
On 24 Oct, 2013, at 10:19 pm, Jerry Krinock <email@hidden> wrote:
>
> 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
_______________________________________________
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