
我做了个实验
@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 是支持的
1 holy_sin 2015-09-21 17:50:32 +08:00 Objective-C 新加的 Generic Type ,这个有官方文档吗 |
2 wanganjun 2015-09-21 17:58:34 +08:00 我感觉 @interface MTTestViewController : MTBaseListViewController <MTTestModel *> 不可能编译通过啊, 我自己在 Xcode 7 上试了, 也确实不行 |
3 expkzb 2015-09-21 18:00:49 +08:00 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. |
5 xi_lin OP |
6 xi_lin OP @holy_sin http://drekka.ghost.io/objective-c-generics/ 这个哥们尝试的比较完整 |
7 wanganjun 2015-09-21 19:52:11 +08:00 可以编译过, 是我忘了导入头文件... |
8 dorentus 2015-09-21 20:03:59 +08:00 via iPad 这个其实是为 Swift 做的功能,此外除了 NSArray 、 NSDictionary 、 NSSet 这三个,其它类型没啥特殊处理,你可以当作没范型这个东西。 |
9 dorentus 2015-09-21 20:09:47 +08:00 via iPad |
10 xi_lin OP @dorentus Runtime 的时候泛型擦除了,编译的时候还是有一定的类型推断和检查的,看这个 http://drekka.ghost.io/objective-c-generics/ |
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 { } |