问题描述
我有一个 NSString
类别类 (NSString+URLEncoding.h
).我遇到了未知的选择器崩溃,因为我调用类别方法的字符串已被 iOS 优化为 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
我从以下方面了解到 iOS 5 中的 NSCFConstantString
与 NSCFString
优化:http://www.cocoanetics.com/2012/03/beware-of-nsstring-optimizations/
I learned of the NSCFConstantString
vs. NSCFString
optimizations in iOS 5 from:http://www.cocoanetics.com/2012/03/beware-of-nsstring-optimizations/
有谁知道我如何让 NSString 类别包含常量字符串,甚至强制 var 成为 NSString/NSCFString
而不是 NSCFConstantString
?
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
?
干杯,Z
-编辑-
- 链接器标志
-ObjC -all_load
都已实现 - NSString+URLEncoding.m 包含在目标编译源中
- NSString+URLEncoding.m 实现了 URLEncodedString 方法.
- 检查是否有僵尸.
我正在向 ShareKit 2.0 添加共享服务
I am adding a sharing service to ShareKit 2.0
标题:
@interface NSString (OAURLEncodingAdditions)
- (NSString *)URLEncodedString;
实现:
@implementation NSString (OAURLEncodingAdditions)
- (NSString *)URLEncodedString
{
NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)self,
NULL,
CFSTR("!*'();:@&=+$,/?%#[]"),
kCFStringEncodingUTF8);
[result autorelease];
return result;
}
推荐答案
链接器存在问题,可能导致其死代码剥离完全忽略任何仅包含 obj-c 类别(或以其他方式未引用)的目标文件).理论上将 -ObjC
标志传递给链接器应该可以解决这个问题,但这似乎并不总是有效.您可以通过提供 -all_load
链接器标志来解决此问题,这将导致链接器始终链接所有目标文件.
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.
请注意,如果您的类别是您在某处包含的子项目或库的一部分,您可能必须在父项目上设置 -all_load
.
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.
更新:我相信 -ObjC
现在是可靠的,并且多年来一直如此,因此您可以停止使用 -all_load
来解决这个问题.
Update: I believe -ObjC
is reliable now and has been for years so you can stop using -all_load
for this issue.
这篇关于iOS 5:使 NSString 类别包括 NSCFConstantString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!