Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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
强制子类实现协议swift_Swift - Fatal编程技术网

强制子类实现协议swift

强制子类实现协议swift,swift,Swift,如何强制子类实现父类中声明的协议 我试过这个: protocol MyProtocol { var myVar : String { get } } class ParentClass: MyProtocol { var myVar = "parent" } class ChildClass: ParentClass { } 但是我的子类并不强迫我重写myVar 这是可能的吗 多谢各位 摩根据我所知,这在Swift中是不可能的。如果尝试遵守父类的协议,则会导致错误“无法使用

如何强制子类实现父类中声明的协议

我试过这个:

protocol MyProtocol {
    var myVar : String { get }
}

class ParentClass: MyProtocol {
    var myVar = "parent"
}

class ChildClass: ParentClass {
}
但是我的子类并不强迫我重写myVar

这是可能的吗

多谢各位


摩根

据我所知,这在Swift中是不可能的。如果尝试遵守父类的协议,则会导致错误“无法使用存储属性重写”。因为协议已经在父类中一致

protocol MyProtocol {
    var myVar : String { get  }
}

class ParentClass: MyProtocol {
    var myVar = "parent"

}

class ChildClass: ParentClass {
    var myVar = "hello"
    // Throws compilation error, "Cannot override with a stored property" since it's already conformed by the parentClass itself.
}
增加:

一般来说,一个接口的多级实现是不可能的,在iOS中,协议应该只在单级实现。但由于您继承了父类,所以childClass具有访问父类成员的范围

class ChildClass: ParentClass, MyProtocol {
    func printValue(){
        println("newvalue : \(myVar)")
        myVar = "hello"
    }

}

希望这对…有帮助

Swift没有这样的功能

您所能做的就是发出运行时错误。您必须使用来覆盖该属性

大概是这样的:

protocol MyProtocol {
    var myVar : String { get }
}

class ParentClass: MyProtocol {
     var myVar:String {
        if self.dynamicType !== ParentClass.self {
            fatalError("subclass must implement myVar")
        }
        return "parent"
     }
}

class ChildClass1: ParentClass {
    override var myVar:String {
        return "hello"
    }
}

class ChildClass2: ParentClass {
    // missing myVar implementation
}

let parent = ParentClass()
parent.myVar // -> "parent"

let child1 = ChildClass1()
child1.myVar // -> "hello"

let child2 = ChildClass2()
child2.myVar // -> fatal error: subclass must implement myVar

我处理这个问题的方法是在类初始值设定项中包含委托参数。请参阅下面的代码:

protocol ProtocolExample {
    func somethingNeedsToHappen()
}

// typical class example with delegate property for the required protocol
class ClassExampleA {
    
    var delegate: ProtocolExample!
    
    init() {
    }
    
    func aCriticalMethodWithUpdates() {
        delegate.somethingNeedsToHappen()
    }
}

// use class example in a view controller.  Can easily forget to invoke the delegate and protocol
class MySampleViewControllerA: UIViewController {
    
    var classExampleA : ClassExampleA!
    
    func loadMyData() {
        classExampleA = ClassExampleA()
    }
}

// an alternative approach for the class is to include the delegate parameter in the initializer.
class ClassExampleB {
    
    var delegate: ProtocolExample!
    
    init(delegateForUpdates: ProtocolExample) {
        delegate = delegateForUpdates
    }
    
    func doSomething() {
        delegate.somethingNeedsToHappen()
    }
}

// go to use it and you're reminded that the parameter is required...
class MySampleViewControllerB: UIViewController {
    
    var classExampleB: ClassExampleB!
    
    func loadMyData() {
        classExampleB = ClassExampleB() // error: Missing argument for parameter 'delegateForUpdates' in call
    }
}

// so to avoid error:
class MySampleViewControllerC: UIViewController {
    
    var classExampleB: ClassExampleB!
    
    func loadMyData() {
        classExampleB = ClassExampleB(delegateForUpdates: <#ProtocolExample#>)
    }
}
协议协议样本{
func一些需要发生的事情()
}
//具有所需协议的委托属性的典型类示例
类示例A{
变量代理:原型示例!
init(){
}
func aCriticalMethodWithUpdates(){
delegate.somethingneedstocome()需要发生的事情
}
}
//在视图控制器中使用类示例。很容易忘记调用委托和协议
类mySampleViewController:UIViewController{
var classExampleA:classExampleA!
func loadMyData(){
classExampleA=classExampleA()
}
}
//该类的另一种方法是在初始值设定项中包含委托参数。
类示例B{
变量代理:原型示例!
init(delegateForUpdates:ProtocolExample){
delegate=delegateForUpdates
}
func doSomething(){
delegate.somethingneedstocome()需要发生的事情
}
}
//去使用它,你会被提醒参数是必需的。。。
类mySampleViewController B:UIViewController{
var classExampleB:classExampleB!
func loadMyData(){
classExampleB=classExampleB()//错误:调用中缺少参数“DelegateForUpdate”的参数
}
}
//因此,为了避免错误:
类mySampleViewController:UIViewController{
var classExampleB:classExampleB!
func loadMyData(){
classExampleB=classExampleB(用于更新的委派:)
}
}

是,它会引发编译错误。所以我必须在我的子类声明中添加我的协议?class-ChildClass:ParentClass,MyProtocol{},它同样具有相同的效果,将抛出相同的错误。请阅读我文章的“添加”部分。阅读后,如果您想要访问两个父类成员并想要实现MyProtocol成员,那么这是不可能的。但是,你可以做上述任何一项,但不能同时做这两项。这是我找到的最合适的答案:)因此+1