問題描述
我有一個 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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!