iOS 5:使NSString类别包括NSCFConstantString? [英] iOS 5: Make NSString Category include NSCFConstantString?

查看:1023
本文介绍了iOS 5:使NSString类别包括NSCFConstantString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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中了解到了 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类别包含Constant字符串甚至强制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

Cheers, Z

-edit -

-edit-


  • 链接器标记 -ObjC -all_load 都已实现

  • NSString + URLEncoding.m包含目标编译源中的ded

  • NSString + URLEncoding.m实现了URLEncodedString方法。

  • 检查僵尸。

  • 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.

我正在为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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆