Objective-C 新加的 Generic Type 好像不支持 block 内的类型传递? - V2EX
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 3920 次点击
    这是一个创建于 3750 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我做了个实验

    @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 7are 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     Solana     2580 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 06:30 PVG 14:30 LAX 22:30 JFK 01:30
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86