Re: Toll-free bridge type at runtime
Re: Toll-free bridge type at runtime
- Subject: Re: Toll-free bridge type at runtime
- From: Marcel Weiher <email@hidden>
- Date: Thu, 2 Apr 2009 22:44:39 -0700
On Apr 2, 2009, at 17:54 , Chris Suter wrote:
On Fri, Apr 3, 2009 at 11:06 AM, Marcel Weiher <email@hidden
> wrote:
As I explained,
Did you?
Yes.
it is trivially possible, because the only Objective-C class
that is the same as its underlying CFType is NSCFArray. So a
simple test
would be [object class] == [NSCFArray class].
Right. So something like this then:
[snip creating using a straight [[NSArray alloc] init];
So, I'm still confused.
That's OK, there seems to be plenty of confusion in this thread.
NSArray's initializers return an NSArray subclass, specifically
NSCFArray, which is the same as the structure returned by
CFArrayCreate(), so your result is entirely expected. As I said, the
difference becomes clear when you create your own NSArray subclass.
If CFArray were actually the same as an NSArray, then if I create my
own NSArray subclass, it should have some relationship to what is
returned by CFArrayCreate(). However, that is not the case.
In addition, there seems to be this idea that because something can be
be handled by a CFArrayXYZ function, it must therefore *be* a
CFArray. This is also not the case. The following test program
illustrates both points:
--------------------------- snip --------------
#import <Foundation/Foundation.h>
@interface MyArray : NSArray
{}
@end
@implementation MyArray
-init { return [super init]; }
-objectAtIndex:(int)i { return [NSString stringWithFormat:@"Object
%d",i]; }
-(int)count { return 3; }
@end
int main(int argc, char *argv[] )
{
id pool=[NSAutoreleasePool new];
MyArray *myArray=[MyArray new];
CFArrayRef b = CFArrayCreate (NULL, NULL, 0, &kCFTypeArrayCallBacks);
NSLog(@"[b class]=%@",[b class]);
NSLog(@"[myArray class]=%@",[myArray class]);
NSLog(@"[b isKindOfClass:[myArray class]]=%d",[b isKindOfClass:
[myArray class]]);
NSLog(@"[myArray isKindOfClass:[b class]]=%d",[myArray isKindOfClass:
[b class]]);
NSLog(@"is it a dictionary? count:
%d",CFDictionaryGetCount( myArray ));
exit(0);
[pool release];
return 0;
}
-------------------------- snip ----------------
Running it yields the following results:
2009-04-02 22:36:43.006 a.out[54919:10b] [b class]=NSCFArray
2009-04-02 22:36:43.008 a.out[54919:10b] [myArray class]=MyArray
2009-04-02 22:36:43.008 a.out[54919:10b] [b isKindOfClass:[myArray
class]]=0
2009-04-02 22:36:43.008 a.out[54919:10b] [myArray isKindOfClass:[b
class]]=0
2009-04-02 22:36:43.009 a.out[54919:10b] is it a dictionary? count: 3
As you can see:
1. The classes returned are different.
2. Neither of these objects are in any subclass relationship to each
other
3. My array can actually be used in a CFDictionaryXYZ function. Does
this mean my array is a dictionary? I think not.
So once again: as Ali and Greg correctly point out, the toll-free
bridging does a great job of hiding any differences between these
underlying objects so you don't have to worry about it in most if not
all practical cases.
However, that does not mean that they are the same.
Cheers,
Marcel
_______________________________________________
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