简体   繁体   English

在Swift类中访问协议属性

[英]Accessing protocol property in Swift class

I am trying to use a protocol to pass an array from one class to another. 我正在尝试使用协议将数组从一个类传递到另一个类。

protocol PinsArray {
var dataArray: [LocationPost] {get set}
}

When I am trying to create a delegate in class, which should receive it does not work. 当我尝试在类中创建一个委托时,该委托将无法正常工作。 I cannot access the property 我无法访问该物业

var delegate = PinsArray.self

Like this: 像这样:

delegate.dataArray

It says that "instance member 'dataArray' cannot be used on type PinArray" 它说“实例成员'dataArray'不能在类型PinArray上使用”

So what do I do wrong? 那我该怎么办呢?

You are assigning the type of PinsArray to delegate instead of assigning an instance of a class that conforms PinsArray . 您正在分配PinsArray类型delegate而不是分配符合PinsArray的类的实例 You would need to implement an a class that conforms to PinsArray and assign an instance of that class to delegate. 您将需要实现一个符合PinsArray的类,并将该类的实例分配给委托。 See the following example: 请参见以下示例:

class SomeClass: PinsArray {
    var dataArray: [LocationPost]
    // ...
}

You would use the class above to create an instance of an object that conforms to PinsArray . 您将使用上面的类创建符合PinsArray的对象的实例。

var delegate = SomeClass()

Then you could use: 然后,您可以使用:

delegate.dataArray

我用错别字声明了委托,它应该像这样:

var delegate: PinsArray?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM