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

2015-09-21 17:21:15 +08:00
 xi_lin

我做了个实验

@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 是支持的

3578 次点击
所在节点    iDev
12 条回复
holy_sin
2015-09-21 17:50:32 +08:00
Objective-C 新加的 Generic Type ,这个有官方文档吗
wanganjun
2015-09-21 17:58:34 +08:00
我感觉 @interface MTTestViewController : MTBaseListViewController <MTTestModel *> 不可能编译通过啊, 我自己在 Xcode 7 上试了, 也确实不行
expkzb
2015-09-21 18:00:49 +08:00
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
2015-09-21 18:17:01 +08:00
@wanganjun 我这没有问题只有 warning 没有 fail 。你的提示是什么?
xi_lin
2015-09-21 18:18:58 +08:00
xi_lin
2015-09-21 18:37:27 +08:00
@holy_sin http://drekka.ghost.io/objective-c-generics/ 这个哥们尝试的比较完整
wanganjun
2015-09-21 19:52:11 +08:00
可以编译过, 是我忘了导入头文件...
dorentus
2015-09-21 20:03:59 +08:00
这个其实是为 Swift 做的功能,此外除了 NSArray 、 NSDictionary 、 NSSet 这三个,其它类型没啥特殊处理,你可以当作没范型这个东西。
dorentus
2015-09-21 20:09:47 +08:00
上面我貌似有点记错了,对 Swift 是如上面的,对 Objective-C 我就不清楚了

xi_lin
2015-09-21 20:58:39 +08:00
@dorentus Runtime 的时候泛型擦除了,编译的时候还是有一定的类型推断和检查的,看这个 http://drekka.ghost.io/objective-c-generics/
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
2015-09-22 11:40:33 +08:00
@wanganjun MTBaseListViewController 里的实现的确没有警告,不过我想做的是在 MTTestViewController 里实现类型检查,所以现在是 MTTestViewController 里有 warning

MTTestViewController : MTBaseListViewController <MTTestModel *> 这个语法你可以看 NSArray (NSExtendedArray)的声明

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/222499

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX