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

2018-09-19 19:56:14 +08:00
 holonunu

大意如:

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

3368 次点击
所在节点    iDev
11 条回复
ai277014717
2018-09-20 09:24:14 +08:00
没看懂 CustomClass 到底是个什么类型
holonunu
2018-09-20 09:41:19 +08:00
@ai277014717 指定类,比如 CustomViewController,但参数不是实例,是类
ai277014717
2018-09-20 10:02:14 +08:00
- (void)addClass:(Class) a{
[[a alloc] init];
}
写了一下编译完全没问题
holonunu
2018-09-20 10:04:19 +08:00
@ai277014717 如果限定类型呢(比如把 Class 换成 UIViewController ),只能在函数内部判断?
ai277014717
2018-09-20 10:27:42 +08:00
Class 可以限定协议
luciferovo909
2018-09-20 11:16:32 +08:00
类对象了解一下
holonunu
2018-09-20 11:31:21 +08:00
@luciferovo909 不能作为参数吧
zjddp
2018-09-20 11:55:36 +08:00
运行时
chipmuck
2018-09-25 09:45:50 +08:00
__kindof UIViewController 看看
cheng4741
2018-09-26 14:43:36 +08:00
前段时间碰到过一样的问题,你可以 Class 后面加协议,如下
```
- (void) addClass:(Class<CustomProtocol>)clazz;
```
这样在你用到 addClass 方法的时候会有参数类型提示__unsafe_unretained Class<CustomProtocol>
但是编译器好像并不检查 clazz 是不是遵守 CustomProtocol,你随便传个其他类型也不会有警告
holonunu
2018-09-27 09:47:33 +08:00
@cheng4741 好的 谢谢

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

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

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

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

© 2021 V2EX