V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
banxi1988
V2EX  ›  iDev

Objective-C Runtime 学习分享 ( 1) class_getName 文档错误

  •  
  •   banxi1988 ·
    banxi1988 · 2015-08-12 21:40:06 +08:00 · 2564 次点击
    这是一个创建于 3183 天前的主题,其中的信息可能已经有所发展或是发生改变。
    class_getName 函数文档
    <https://developer.apple.com/library/prerelease/watchos/documentation/Cocoa/Reference/ObjCRuntimeRef/#//apple_ref/c/func/class_getName>;
    说的是:
    > The name of the class, or the empty string if cls is Nil.

    这一句 ”the empty string if cls is Nil“ 不对。
    实际上不是字符串 "nil"
    查看了最新开源的代码 ,也确实返回的是 "nil"

    ```objc
    const char *class_getName(Class cls)
    {
    if (!cls) return "nil";
    assert(cls->isRealized() || cls->isFuture());
    return cls->demangledName();
    }
    ```

    最近想到的一个学习 Objective-C Runtime 的学习方式就是为 其 API 编写测试简单的测试用例。
    如 `class_getName`

    ```objc

    - (void)test_class_getName{
    XCTAssert(strcmp("Demo", class_getName([Demo class])) == 0);
    const char * actual = class_getName(nil);
    XCTAssert(strcmp("nil", actual) == 0);
    }
    ```
    5 条回复    2015-08-13 16:31:31 +08:00
    fengjianxinghun
        1
    fengjianxinghun  
       2015-08-13 09:06:09 +08:00
    不看不知道。。。非常有用。。
    kepenj
        2
    kepenj  
       2015-08-13 11:41:44 +08:00
    可以发到他们官方论坛 去嘲讽他们的工程师~~2333333
    black
        3
    black  
       2015-08-13 13:19:19 +08:00 via iPhone
    请好好理解文档,文档描述的是参数为空的话返回空字符串,参数是Class类型,空的Class是Nil而不是nil,文档是正确的。
    banxi1988
        4
    banxi1988  
    OP
       2015-08-13 16:26:12 +08:00
    @black 一开始我也是这样想的, 但是我测试的时候,发现他们是一样的。
    查看 objc.h 头文件,发现他们其实是同样的宏定义。

    ```c
    #ifndef Nil
    # if __has_feature(cxx_nullptr)
    # define Nil nullptr
    # else
    # define Nil __DARWIN_NULL
    # endif
    #endif

    #ifndef nil
    # if __has_feature(cxx_nullptr)
    # define nil nullptr
    # else
    # define nil __DARWIN_NULL
    # endif
    #endif
    ```
    black
        5
    black  
       2015-08-13 16:31:31 +08:00 via iPhone
    @banxi1988 是的,底层实现是一样的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3283 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 10:23 · PVG 18:23 · LAX 03:23 · JFK 06:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.