Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果在扩展Swift5.1 Xcode11.3中声明,则子类无法访问便利初始化_Swift_Swift5.1_Xcode11.3_Xcframework - Fatal编程技术网

如果在扩展Swift5.1 Xcode11.3中声明,则子类无法访问便利初始化

如果在扩展Swift5.1 Xcode11.3中声明,则子类无法访问便利初始化,swift,swift5.1,xcode11.3,xcframework,Swift,Swift5.1,Xcode11.3,Xcframework,我制作XC框架。这个代码在里面。我将这个xcframework链接到应用程序并使用它,但MyButton类并没有方便的init。XCode 11.3创建MyButton的扩展,而不是UIButton import UIKit public class MyButton: UIButton {} public extension UIButton { var someProperty: Int { 1 } // visible in xcframewor

我制作XC框架。这个代码在里面。我将这个xcframework链接到应用程序并使用它,但MyButton类并没有方便的init。XCode 11.3创建
MyButton
扩展
,而不是
UIButton

    import UIKit

    public class MyButton: UIButton {}

    public extension UIButton {
        var someProperty: Int { 1 } // visible in xcframework

        convenience init(label: String) { // NOT visible in xcframework
            self.init()
        }
    }
在应用程序中,使用

public extension MyButton {
    var someProperty: Int {
        return 1
    }

    convenience init(label: String) { 
        self.init()
    }
}
编辑:

即使是子类
MyButton
init(label:)
也将在子类中可用,直到您定义自己的初始值设定项

MyButton(label: "Button_Label")
您可以在主项目中访问
init(标签:)
,如

public class MySuperButton: MyButton {

}

便利初始化中需要公共标志。这将允许init在xcframework中可见

试试这个

MySuperButton(label: "Super Button")
但是,您正在更新UIButton,而不是您声明的类MyButton。
确保这正是您想要做的。

整个扩展都是公共的。将ini(标签:)显式标记为public不是必需的。public标志没有帮助,如果一旦我声明MyButton的子类,例如MySuperButton,它就不会有这个便利init。@Alexeyneastv在定义子类“自己的初始化器”之前,您可以访问子类中的
便利init
。我已经编辑了答案。
import UIKit

public class MyButton: UIButton {}

public extension UIButton {
    var someProperty: Int { 1 } // visible in xcframework

    public convenience init(label: String) { // NOT visible in xcframework
        self.init()
    }
}