ios - 从 Swift 调用 Objective C 自定义初始化函数
问题描述
A 类是用 Objective C 编写的,并且有一个自定义的 init 函数
@interface A ()
....
@end
@implementation A
- (id)customInitImplementedInA
{
...
return self;
}
B 类继承自 A 类,并以下列方式使用此自定义初始化:
@interface B : A ()
....
@end
@implementation B
+(instancetype)instanceB{
B *b = [[B alloc] customInitImplementedInA];
...
return b;
}
现在我想创建一个用Swift编写的 C 类,它继承自 A 并使用相同的 init 函数。我怎么做?
class C: A {
//How do I use customInitImplementedInA here?
}
解决方案
您应该能够通过super.methodname
语法来做到这一点。
class C: A {
init() {
super.customInitImplementedInA()
// Any extra initialization for C goes here.
}
}
推荐阅读
- python - 从十六进制颜色代码转换为 RGB 时出现无效文字错误
- html - 如何找到使用 XPath 找到的 HTML 元素的内容(Selenium with VBA)
- c - 在 C 中检查 NULL 的多个函数参数的有效方法
- javascript - 显示未定义的随机密码生成器
- c# - 二进制比较 .NET 程序集 - 忽略版本和时间戳
- c++ - C++。如何获得正确的输出?
- html - 如何在单选按钮之间添加空格?(HTML)
- swift - 你如何阻止视图阴影到达它一侧的另一个视图控制器?
- javascript - .then() 优先于 Promise 之外的代码?
- android - “/system/bin/sh: 语法错误:'(' 意外”在 ADB 中指定文件时