Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 5: Make NSString Category include NSCFConstantString?

I have an NSString category class (NSString+URLEncoding.h). I am running into and unknown selector crash, because the string I am calling the category method has been optimized into an NSCFConstantString by iOS.

-[__NSCFConstantString URLEncodedString]: unrecognized selector sent to instance 0x290174

I learned of the NSCFConstantString vs. NSCFString optimizations in iOS 5 from: http://www.cocoanetics.com/2012/03/beware-of-nsstring-optimizations/

Is anyone aware of how I can get the NSString category to include the Constant strings or even force the var to be an NSString/NSCFString and not an NSCFConstantString?

Cheers, Z

-edit-

  • Linker flags -ObjC -all_load are both already implemented
  • NSString+URLEncoding.m is included in the targets compile sources
  • NSString+URLEncoding.m implements the URLEncodedString method.
  • Checked for zombies.

I am adding a sharing service to ShareKit 2.0

header:

@interface NSString (OAURLEncodingAdditions)

- (NSString *)URLEncodedString;

implementation:

@implementation NSString (OAURLEncodingAdditions)

- (NSString *)URLEncodedString 
{
    NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                           (CFStringRef)self,
                                                                           NULL,
                                                                           CFSTR("!*'();:@&=+$,/?%#[]"),
                                                                           kCFStringEncodingUTF8);
    [result autorelease];
    return result;
}
like image 214
Zaid Daghestani Avatar asked Sep 10 '12 20:09

Zaid Daghestani


People also ask

How do I append to NSString?

Once a string has been initialized using NSString, the only way to append text to the string is to create a new NSString object. While doing so, you can append string constants, NSString objects, and other values.

Is NSString UTF 8?

An NSString object can be initialized from or written to a C buffer, an NSData object, or the contents of an NSURL . It can also be encoded and decoded to and from ASCII, UTF–8, UTF–16, UTF–32, or any other string encoding represented by NSStringEncoding .

What is difference between string and NSString in Swift?

The Swift string is one character long, as expected. The NSString says it has a length of seven — this matches with the length of the Swift string's utf16 view, since NSStrings are backed by UTF-16: 09:02 The Swift string's unicodeScalars view returns a count of four.

How do I check if a string contains a character in Objective C?

To check if a string contains another string in objective-c, we can use the rangeOfString: instance method where it returns the {NSNotFound, 0} if a 'searchString' is not found or empty (""). Output: string contains you!


3 Answers

There's an issue with the linker that can cause its dead-code stripping to completely omit any object files that only contain obj-c categories (or that are otherwise unreferenced). Theoretically passing the -ObjC flag to the linker should fix this, but that doesn't seem to always work. You can work around this issue by providing the -all_load linker flag, which will cause the linker to always link in all object files.

Note that you might have to set -all_load on the parent project if your category is part of a sub-project or library that you-re including somewhere.

Update: I believe -ObjC is reliable now and has been for years so you can stop using -all_load for this issue.

like image 191
Lily Ballard Avatar answered Sep 18 '22 12:09

Lily Ballard


Just spent 30 minutes figuring out exactly the same issue. After fiddling with linker I found out that the category wasn't present in Compile Sources list in my target's Build Phases. Be sure to check it's there.

like image 27
Rudolf Adamkovič Avatar answered Sep 18 '22 12:09

Rudolf Adamkovič


__NSCFConstantString is a subclass of NSString, so any categories on NSString apply to __NSCFConstantString too.

Either you're not linking in your category, or your category doesn't define a URLEncodedString method in its @implementation.

like image 20
rob mayoff Avatar answered Sep 20 '22 12:09

rob mayoff