On May 13, 2009, at 11:27 , Mike Mangino wrote:


I'm noticing something related that is causing my problems. I have mocking working for custom types. When I try to change the class of a string, however, I get a crash.

-(void) testImplementationOnString {
        NSString *s = [NSString stringWithString:@"a test string"];
Class subclass = objc_allocateClassPair([s class], "MyStringMock", 0);
        objc_registerClassPair(subclass);
        //object_setClass(s, subclass);

}

If you uncomment the last line this crashes. Is that to be expected? I'm assuming it happens for toll free bridged types. Is that true? I've tried with NSString and NSArray and both cause a crash.

I think your suspicion is correct. What's the crash? Infinite recursion? The CF* functions check wether an object is exactly of the class that they are expecting, for example NSCFArray. If that's not the case, they will send a message instead. For a NSCFArray subclass, this will try to call the corresponding CF* function.

So I don't think you'll have much joy in trying to subclass any of the toll-free bridged classes. I guess Apple didn't include headers for the NSCF* classes for a reason...

I've got a workaround in place, but I'd love to be able perform the same mocking on these types. My code is available at http://github.com/mmangino/OCMock/tree/master

Marcel


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to