Does a Category applied to NSString also apply to NSCFString via the "toll-free bridge"?
We're integrating a library into an iPhone app which appears to use the google toolbox for iPhone internally.
The google toolbox adds a method gtm_stringBySanitizingAndEscapingForXML
to NSString.
The problem is, whenever we attempt to make a call to this library we get
[NSCFString gtm_stringBySanitizingAndEscapingForXML]: unrecognized selector sent to instance 0x272478
So it appears the library is calling that method on a NSCFString, to which the category does not apply. So... is it the case that the category will not apply across the toll-f开发者_开发知识库ree bridge to CoreFoundation classes? If that's the case then we at least know why it's blowing up. Figuring out how to fix it is a different matter.
Categories applied to NSString do apply to NSCFString as well, because NSCFString is a subclass of NSMutableString which is a subclass of NSString.
But have you actually included the Google Toolbox library (GTMNSString+XML.m
)?
NSCFString
class is not a subclass of NSMutableString
... It's just another class of NSString
cluster. So if you have a NSCFString
foo var and you test this:
BOOL isNSString = [foo isKindObClass:[NSString class]];
You will get that isNSString
is NO
.
I'm experiencing some problems because I've created a NSString
category and I don't know how to apply the new methods when the class is a NSCFString
or any other class from that cluster...
EDIT:
Ok, I found the solution. Although those tests returned NO
:
[myString isKindOfClass:[NSString class]];
[myString respondsToSelector:@selector(myNSStringCategorySelector:)];
I forced the execution of the method for the NSCFString
class, and it worked correctly!!
I hope it'll help someone!
Sounds like the implementation of this category is not linked into your program. Assuming that your library is compiled as a static library you might need to add the -ObjC
linker flag to your project. For more information take a look at this technote. The linker bug mentioned in there should be fixed with the latest Xcode release.
精彩评论