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
holonunu
V2EX  ›  iDev

OC 中可以指定 CustomClass 的作为参数吗?

  •  
  •   holonunu · Sep 19, 2018 · 4638 views
    This topic created in 2969 days ago, the information mentioned may be changed or developed.

    大意如:

    • (void)addClass:(CustomClass)class;

    目前想到的办法:传 className 再进行判断绕开这个问题。

    11 replies  •  2018-09-27 09:47:33 +08:00
    ai277014717
    ai277014717
    Sep 20, 2018
    1
    没看懂 CustomClass 到底是个什么类型
    holonunu
    holonunu
    OP
    Sep 20, 2018
    2
    @ai277014717 指定类,比如 CustomViewController,但参数不是实例,是类
    ai277014717
    ai277014717
    Sep 20, 2018
    3
    - (void)addClass:(Class) a{
    [[a alloc] init];
    }
    写了一下编译完全没问题
    holonunu
    holonunu
    OP
    Sep 20, 2018
    4
    @ai277014717 如果限定类型呢(比如把 Class 换成 UIViewController ),只能在函数内部判断?
    ai277014717
    ai277014717
    Sep 20, 2018
    5
    Class 可以限定协议
    luciferovo909
    luciferovo909
    Sep 20, 2018
    6
    类对象了解一下
    holonunu
    holonunu
    OP
    Sep 20, 2018
    7
    @luciferovo909 不能作为参数吧
    zjddp
    zjddp
    Sep 20, 2018
    8
    运行时
    chipmuck
    chipmuck
    Sep 25, 2018
    9
    __kindof UIViewController 看看
    cheng4741
    cheng4741
    Sep 26, 2018
    10
    前段时间碰到过一样的问题,你可以 Class 后面加协议,如下
    ```
    - (void) addClass:(Class<CustomProtocol>)clazz;
    ```
    这样在你用到 addClass 方法的时候会有参数类型提示__unsafe_unretained Class<CustomProtocol>
    但是编译器好像并不检查 clazz 是不是遵守 CustomProtocol,你随便传个其他类型也不会有警告
    holonunu
    holonunu
    OP
    Sep 27, 2018
    11
    @cheng4741 好的 谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2596 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 14:08 · PVG 22:08 · LAX 06:08 · JFK 09:08
    ♥ Do have faith in what you're doing.