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

Objective-C 新加的 Generic Type 好像不支持 block 内的类型传递?

  •  
  •   xi_lin · 2015-09-21 17:21:15 +08:00 · 3567 次点击
    这是一个创建于 3151 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我做了个实验

    @interface MTBaseListViewController <T> : UIViewController
    
    - (void )requestWithBlock1:(void (^)(T data ))block;
    - (void )requestWithBlock2:(void (^)(MTListPagingModel<T> *data ))block;
    
    @end;
    

    声明没有问题。

    在使用时:

    @interface MTTestViewController : MTBaseListViewController <MTTestModel *>
    @end
    
    @implementation MTTestViewController
    
    - (void )requestWithBlock1:(void (^)(MTTestModel *data ))block {
    }
    
    - (void )requestWithBlock2:(void (^)(MTListPagingModel<MTTestModel *> *data ))block {
    }
    
    @end
    

    都会提示 Conflicting parameter types in implementation ....

    block 内的声明还是不支持泛型吗?

    但是我看到NSArray<ObjectType> (NSExtendedArray )里的 enumerateObjectsUsingBlock 是支持的

    12 条回复    2015-09-22 11:40:33 +08:00
    holy_sin
        1
    holy_sin  
       2015-09-21 17:50:32 +08:00
    Objective-C 新加的 Generic Type ,这个有官方文档吗
    wanganjun
        2
    wanganjun  
       2015-09-21 17:58:34 +08:00
    我感觉 @interface MTTestViewController : MTBaseListViewController <MTTestModel *> 不可能编译通过啊, 我自己在 Xcode 7 上试了, 也确实不行
    expkzb
        3
    expkzb  
       2015-09-21 18:00:49 +08:00   ❤️ 1
    The lightweight generics introduced in Xcode 7 are just compile time hints to help the compiler raise warnings, but at run time you get the same old behavior with your variable being just NSArrays of ids.
    xi_lin
        4
    xi_lin  
    OP
       2015-09-21 18:17:01 +08:00
    @wanganjun 我这没有问题只有 warning 没有 fail 。你的提示是什么?
    xi_lin
        6
    xi_lin  
    OP
       2015-09-21 18:37:27 +08:00
    @holy_sin http://drekka.ghost.io/objective-c-generics/ 这个哥们尝试的比较完整
    wanganjun
        7
    wanganjun  
       2015-09-21 19:52:11 +08:00
    可以编译过, 是我忘了导入头文件...
    dorentus
        8
    dorentus  
       2015-09-21 20:03:59 +08:00 via iPad
    这个其实是为 Swift 做的功能,此外除了 NSArray 、 NSDictionary 、 NSSet 这三个,其它类型没啥特殊处理,你可以当作没范型这个东西。
    dorentus
        9
    dorentus  
       2015-09-21 20:09:47 +08:00 via iPad
    上面我貌似有点记错了,对 Swift 是如上面的,对 Objective-C 我就不清楚了

    xi_lin
        10
    xi_lin  
    OP
       2015-09-21 20:58:39 +08:00
    @dorentus Runtime 的时候泛型擦除了,编译的时候还是有一定的类型推断和检查的,看这个 http://drekka.ghost.io/objective-c-generics/
    wanganjun
        11
    wanganjun  
       2015-09-22 09:46:21 +08:00
    MTTestViewController : MTBaseListViewController <MTTestModel *> 这个语法怎么来的?
    我觉得是想指定 MTBaseListViewController 里面的 T, 不过这个语法我倒是第一次看到.

    但是这个指定好像没有作用, 把 MTBaseListViewController 里的实现换成下面的就没有警告了
    - (void)requestWithBlock:(void (^)(id data))block {

    }

    - (void)requestWithBlock2:(void (^)(MTListPagingModel<id> *data))block {

    }
    xi_lin
        12
    xi_lin  
    OP
       2015-09-22 11:40:33 +08:00
    @wanganjun MTBaseListViewController 里的实现的确没有警告,不过我想做的是在 MTTestViewController 里实现类型检查,所以现在是 MTTestViewController 里有 warning

    MTTestViewController : MTBaseListViewController <MTTestModel *> 这个语法你可以看 NSArray (NSExtendedArray)的声明
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1063 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:25 · PVG 03:25 · LAX 12:25 · JFK 15:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.