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

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

  •  
  •   holonunu · 2018-09-19 19:56:14 +08:00 · 3351 次点击
    这是一个创建于 2035 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大意如:

    • (void)addClass:(CustomClass)class;

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

    11 条回复    2018-09-27 09:47:33 +08:00
    ai277014717
        1
    ai277014717  
       2018-09-20 09:24:14 +08:00
    没看懂 CustomClass 到底是个什么类型
    holonunu
        2
    holonunu  
    OP
       2018-09-20 09:41:19 +08:00
    @ai277014717 指定类,比如 CustomViewController,但参数不是实例,是类
    ai277014717
        3
    ai277014717  
       2018-09-20 10:02:14 +08:00
    - (void)addClass:(Class) a{
    [[a alloc] init];
    }
    写了一下编译完全没问题
    holonunu
        4
    holonunu  
    OP
       2018-09-20 10:04:19 +08:00
    @ai277014717 如果限定类型呢(比如把 Class 换成 UIViewController ),只能在函数内部判断?
    ai277014717
        5
    ai277014717  
       2018-09-20 10:27:42 +08:00
    Class 可以限定协议
    luciferovo909
        6
    luciferovo909  
       2018-09-20 11:16:32 +08:00
    类对象了解一下
    holonunu
        7
    holonunu  
    OP
       2018-09-20 11:31:21 +08:00
    @luciferovo909 不能作为参数吧
    zjddp
        8
    zjddp  
       2018-09-20 11:55:36 +08:00
    运行时
    chipmuck
        9
    chipmuck  
       2018-09-25 09:45:50 +08:00
    __kindof UIViewController 看看
    cheng4741
        10
    cheng4741  
       2018-09-26 14:43:36 +08:00
    前段时间碰到过一样的问题,你可以 Class 后面加协议,如下
    ```
    - (void) addClass:(Class<CustomProtocol>)clazz;
    ```
    这样在你用到 addClass 方法的时候会有参数类型提示__unsafe_unretained Class<CustomProtocol>
    但是编译器好像并不检查 clazz 是不是遵守 CustomProtocol,你随便传个其他类型也不会有警告
    holonunu
        11
    holonunu  
    OP
       2018-09-27 09:47:33 +08:00
    @cheng4741 好的 谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5552 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 08:45 · PVG 16:45 · LAX 01:45 · JFK 04:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.