【问题标题】:Creating NSString results in null创建 NSString 结果为 null
【发布时间】:2016-04-06 21:27:17
【问题描述】:

我正在尝试初始化NSString。我尝试了initWithString:,但这变得无效,因为NSString 变成了NSPlaceholderString。我切换到UTF8string,但它返回一个空值。

代码如下:

- (void) test:(NSString*)thing {
    id (*p_objc_msgSend)(id p, SEL sel, ...);
    id (*p_objc_getClass)(const char *s);
    Class class;

    p_objc_getClass = objc_getClass;
    p_objc_msgSend = objc_msgSend;

    NSString *s;

    class = (*p_objc_getClass)( "NSString");
    //SEL init = @selector(initWithString:);
    s = (*p_objc_msgSend)(class, @selector(alloc));
    s = (*p_objc_msgSend)(s, @selector(initWithUTF8String:), "AAA");

    NSLog(@"Value of %@", s);
}

【问题讨论】:

  • 你为什么要以这种奇怪的方式做这件事?为什么不简单地将以上所有内容替换为NSString *s = @"AAA";
  • NSPlaceholderString 有什么问题?
  • 这种做法不是创建字符串
  • 第一行返回null

标签: objective-c nsstring objective-c-runtime


【解决方案1】:

NSString 是一个具有多个私有子类的类簇。 NSPlaceholderString 是 NSString 的子类,因此您可以继续使用该对象作为任何其他 NSString。

【讨论】:

    猜你喜欢
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多