Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class extensions and frameworks

I have develop my own framework that contains useful classes/methods that I often use in my apps. Recently, I've added a class extension for NSString "NSString+Extensions.h/m" to add my own methods. Example :

  • NSString+Extensions.h
@interface NSString (Extensions)
- (NSString *)removeDiacritics; 
@end
  • NSString+Extensions.m
#import "NSString+Extensions.h"

@implementation NSString (Extensions)

- (NSString *)removeDiacritics
{ 
    return [[[NSString alloc] initWithData:[self dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES] encoding:NSASCIIStringEncoding] autorelease];
}

@end

I successfully compile my framework. But when I try to use one of the functions of this class extension in any application :

  • AppDelegate.m
// CUtils is the name of the framework. CUtils.h contains #import of all header files
// contained in my framework
#import <CUtils/CUtils.h>


  @implementation AppDelegate

    ...

    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
        /*
         Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previouslyin the background, optionally refresh the user interface.
         */

        NSString *toto = @"Je suis une chaîne avec des caractères spéciaux";
        NSLog(@"%@", toto);
        NSLog(@"%@", [toto removeDiacritics]);
    }

...

I get the following error :

2012-01-31 17:01:09.921 TestCUtils[4782:207] Je suis une chaîne avec des caractères spéciaux 2012-01-31 17:01:09.924 TestCUtils[4782:207] -[__NSCFConstantString removeDiacritics]: unrecognized selector sent to instance 0x340c

But if I add my class extension directly in the application (outside of my framework), it works fine...

Any hint?

** EDIT **

As some of you have asked, I've added -all_load and -ObjC options in 'Other Linker Flags', but the issue remains.

enter image description here

like image 682
Niko Avatar asked Jan 31 '12 16:01

Niko


2 Answers

Take a look at this technical Q&A that explains the -ObjC and -all_load options that @Ell Neal mentions.

Note The linker options need to be set on the project that is linking the framework (i.e. the client of the framework) not the framework itself. From your screenshot it looks like you are setting the option on your framework project because I can see the source file NString+Extensions.m on the left.

like image 175
Robin Summerhill Avatar answered Oct 20 '22 23:10

Robin Summerhill


You need to add -ObjC to Other Linker Flags in your build settings. If this doesn't work, try adding -all_load

like image 28
Ell Neal Avatar answered Oct 20 '22 23:10

Ell Neal