Linker cannot find a class that should be there
I have a project that uses the Core Telephony framework. Recently my code stopped working on a CTCarrier
category, the linker complains that it can’t find the CTCarrier
class:
Undefined symbols:
开发者_运维问答 "_OBJC_CLASS_$_CTCarrier", referenced from:
l_OBJC_$_CATEGORY_CTCarrier_$_Foo in CTTests.o
ld: symbol(s) not found
This is a sample code that triggers the error above:
#import <CoreTelephony/CTCarrier.h>
@interface CTCarrier (Foo)
- (void) doFoo;
@end
@implementation CTCarrier (Foo)
- (void) doFoo {}
@end
If I change the category to class extension, the code suddenly builds:
#import <CoreTelephony/CTCarrier.h>
@interface CTCarrier ()
- (void) doFoo;
@end
@implementation CTCarrier
- (void) doFoo {}
@end
What’s going on? Sample code on GitHub.
There is a bug in 4.2 that doesn't allow the direct creation of a CTCarrier object, the proper way to access CTCarrier is via the CTTelephonyNetworkInfo object like so:
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
CTTelephonyNetworkInfo *telephony = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = telephony.subscriberCellularProvider;
[telephony release];
In the first example you don't really are implementing CTCarrier class but only add a method to it. The categories provide a way to add methods to an already defined implementation.
精彩评论