3

I have an ARC project setup in xcode. To that project I added a non-arc static library. Everything works and builds find. When I run the project I get this error.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString encodedURLParameterString]: unrecognized selector sent to instance 0x4a030'

from this code:

NSString * value = [params objectForKey:key];
[_params setObject:[value encodedURLParameterString] forKey:key];

that's trying to call this

@implementation NSString (OAURLEncodingAdditions)

- (NSString *)encodedURLParameterString {
    NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                           (CFStringRef)self,
                                                                           NULL,
                                                                           CFSTR(":/=,!$&'()*+;[]@#?"),
                                                                           kCFStringEncodingUTF8);
    return [result autorelease];
}

@end

The error seems to be saying that it can't find this function. Of course it compiles just fine and I can jump to the definition just fine. The string value is a constant string which of course inherits from NSString. This works just fine in the other project that I pulled it from. It finds this function perfectly so as an Xcode newbie I'm at my wits end.

The file is included in the compile sources. There aren't any errors and it compiles and runs other code just fine. I seem to be missing one small part and I just can't understand what it is. If you can help please do.

Kyle
  • 1,172
  • 1
  • 8
  • 23

3 Answers3

4

Use the -ObjC and / or -all_load linker flags in your project. Reference: What does the -all_load linker flag do?

Community
  • 1
  • 1
Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
1
// file NSString+URLEncoding.m
#import <Foundation/Foundation.h>
@interface NSString (OAURLEncodingAdditions)
- (NSString *)encodedURLParameterString;
@end

// file NSString+URLEncoding.h - this file is marked with -fno-objc-arc
#import "NSString+URLEncoding.h"
@implementation NSString (OAURLEncodingAdditions)
- (NSString *)encodedURLParameterString {
    NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                           (CFStringRef)self,
                                                                           NULL,
                                                                           CFSTR(":/=,!$&'()*+;[]@#?"),
                                                                           kCFStringEncodingUTF8);
    return [result autorelease];
}
@end

Given the code above, this code should work:

#import "NSString+URLEncoding.h"
// ...
NSString * value = @"xxxxx";
value = [value encodedURLParameterString];
Jano
  • 62,815
  • 21
  • 164
  • 192
  • I wasn't calling the function directly. I've tried to import this and then call the function directly but it still gives me the same error. – Kyle Sep 12 '12 at 21:51
  • 1
    Check what is the name of the file containing the method encodedURLParameterString. Let's say it is "NSString+URLEncoding.m". Then go to the file where the error happens and write this at the top: `#import NSString+URLEncoding.h"`. Then try again. – Jano Sep 12 '12 at 22:12
  • This does not work. I think it has something to do with using a non arc selector – Kyle Sep 12 '12 at 22:15
  • it's unrelated to -fno-objc-arc. I edited the answer with an example. – Jano Sep 12 '12 at 23:12
0

Not sure how to do this with a secondary project so I just removed the other project and added compiler flags

http://blog.evanmulawski.com/?p=36

Kyle
  • 1,172
  • 1
  • 8
  • 23