2

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.

JOM
  • 8,139
  • 6
  • 78
  • 111
zoul
  • 102,279
  • 44
  • 260
  • 354

2 Answers2

1

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];
Greg Martin
  • 5,074
  • 3
  • 34
  • 35
0

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.

Bruno Berisso
  • 1,091
  • 11
  • 33
  • I know how categories work, at least I hope so :) The problem is that the linker complains that it cannot find a class that I am adding a category to. When I switch to a class extension instead of the category, the linker stops complaining. That’s weird. – zoul Nov 25 '10 at 11:46
  • The class is implemented in other .m file? I'm referring to '@implementation CTCarrier' – Bruno Berisso Nov 25 '10 at 12:47
  • The `CTCarrier` class comes from the Core Telephony framework. – zoul Nov 25 '10 at 12:51
  • Interesting... Can you paste in your question the gcc line that's compile your category file? – Bruno Berisso Nov 25 '10 at 13:15
  • I’ve uploaded a sample project to [GitHub](https://github.com/zoul/Xcode-Linker-Issue). – zoul Nov 25 '10 at 13:58
  • Maybe here are a clue: http://stackoverflow.com/questions/2567498/objective-c-categories-in-static-library – Bruno Berisso Nov 25 '10 at 15:31