gpt4 book ai didi

swift - 在 Swift 中为具有命名冲突的 Objective-C 类覆盖 init 方法

转载 作者:行者123 更新时间:2023-11-28 07:06:08 25 4
gpt4 key购买 nike

我正在尝试从 Bluemix 上的移动数据 SDK 继承 IBMDataObjectIBMData 框架是用 Objective-C 编写的,所以我试图覆盖的 init 方法如下所示:

- (instancetype)initWithClass:(NSString *)classname;

在 Swift 中使用它,它被转换为:

init!(withClass classname: String!) {
super.init(withClass: classname)
}

这一定与 Swift 中的命名约定检查冲突,因为我会得到错误:

enter image description here

在 init 方法名称中包含单词 class 似乎会导致问题,因为自动修复会删除 withClass 并放入关键字 class

我不确定是否有解决方法,也许是警告抑制?或者 IBMDataObject init 方法是否需要更改它的名称?

最佳答案

我遇到了同样的错误,通过更改覆盖方法修复了它:

required override init() {
super.init(withClass: "Item")
}

因为 init 方法需要接收字符串参数。 <<(NSString *)类名;>>

还将 init 方法更改为:

init(className: String){
super.init(withClass: className)
}

希望对您有所帮助。

关于swift - 在 Swift 中为具有命名冲突的 Objective-C 类覆盖 init 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30565246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com