3

I have the following problem that I do not succeed to solve. Your help will be appreciated.

To calculate a md5 from a NSS tring, a category has been defined by :

NSString-md5.h

@interface NSString (MD5)
- (NSString *)MD5String;
@end

NSString-md5.m

#import <CommonCrypto/CommonDigest.h>
#import "NSString-md5.h"
@implementation NSString(MD5)
- (NSString *)MD5String
    {is
    const char *cstr = [self UTF8String];
    unsigned char result[16];    
    CC_MD5( cstr, strlen(cstr), result );
    return [NSString stringWithFormat:
        @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
        result[0], result[1], result[2], result[3], 
        result[4], result[5], result[6], result[7],
        result[8], result[9], result[10], result[11],
        result[12], result[13], result[14], result[15]
        ];  
    }
@end

calling code (extract)

#import "NSString-md5.h"
...
NSLog(@"1 %@ ",message.text);
[message.text MD5String];

aborts with error message :

2013-03-03 19:06:14.104 coffreFort[1230:11303] 1 aze 
2013-03-03 19:06:22.777 coffreFort[1230:11303] -[__NSCFString MD5String]: unrecognized selector sent to instance 0x71e62b0
2013-03-03 19:06:22.777 coffreFort[1230:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString MD5String]: unrecognized selector sent to instance 0x71e62b0'

message.text is an IBOutlet defined by

@property (weak, nonatomic) IBOutlet UITextField *message;
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140

1 Answers1

10

It's an issue with CLANG (which they promised would be fixed) where categories often go unloaded, especially in the context of frameworks or libraries.. You have to pass in the linker flags -all_load (or -force_load) in addition to -ObjC to force the linker to include them with your binary.

CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • 1
    Only adding `-ObjC` in the Other Linker Flags resolved my same issue. – Lucien Jul 03 '14 at 18:19
  • This is not an issue with clang as you get exactly the same result with gcc. This is an issue with how Obj-C works and what categories actually are under the hood. Categories are not symbols in the classical C sense and are added dynamically at run time, hence a compiler cannot know if they are referenced at link time. For a more detailed explanation, see here https://stackoverflow.com/a/22264650/15809 – Mecki Jan 24 '20 at 20:16