何时在Swift中使用协议 [英] When to use protocol in Swift

查看:140
本文介绍了何时在Swift中使用协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天问一个问题,因为我今天有点迷茫。它是关于Swift和协议的,还有关于面向协议的编程(POP)的更多信息。

I ask a question today because I’m little bit lost today. It’s about Swift and protocol and more about Protocol Oriented Programming (POP).

我读了有关它的文章,甚至是一本书,但我仍然感到困惑。似乎每个人都说Protocol是一个很好的工具,等等,但我不太了解它的功能。

I read articles about it, even a book but I’m still confused. Everybody seems to say that Protocol is a great tool and so on but I don’t really understand its power.

我有一个问题,因为我正在编码一个类卷是作为对象的卷的表示。假设

I’ve a question because I’m coding a class Volume which is the representation of a volume as an object. Let’s say

struct Volume {
  var value: Float = 1
  var isLogScale: Bool = false
  var maxLogVolume: Float = 6.0 // value in dB
  var maxLinearVolume: Float = 1.0
  let dynamicRange: Float = 50.0


  func convertLinearToLog() -> Float {
    // Do some maths
  }

  func otherFunction() {
    print("Hello, I'm the most useless function in the world.")
  }
}

这是一个典型的没什么特别的类。

This is a typical class nothing special.

但是……我应该为我们提供一个更好的协议吗?

But… Should I better us a protocol like this:

protocol VolumeProtocol {
  var value: Float {get set}
  var isLogScale: Bool {get set}
  var maxLogVolume: Float {get set}
  var maxLinearVolume: Float {get set}
  let dynamicRange: Float {get}

  func convertLinearToLog() -> Float
  func otherFunction()
}

然后重新实现类似

struct MyVolumeClass: VolumeProtocol {
// Same thing as before
}

我并没有真正回答这个问题,所以如果您能帮助我何时使用协议,何时不可以,我将不胜感激

I don’t really manage to answer to this question, so if you could help me when to use protocol and when not I would appreciate.

推荐答案

协议有多个用例,但请以这种方式考虑:

Protocols have more than one use case, but think of it this way:


  • 对于类,协议提供了与类层次结构分开的轻量级继承层次结构。给定动物类及其子类猫,狗,鸟和昆虫,您如何指定仅鸟和昆虫共享 fly 方法?

对于结构,协议提供了一个继承层次结构,否则将完全丢失!结构没有上层结构。因此,给定一个结构鸟和一个结构昆虫,您如何指定它们共享 fly 方法?

For structs, protocols provide an inheritance hierarchy that would otherwise be missing entirely! A struct has no superstruct. So, given a struct Bird and a struct Insect, how would you specify that they share a fly method?

现在,您可以回答Bird和Insect恰好具有 fly 方法,这就是故事的结尾。但是,当您需要说所有具有 fly 方法的所有类型集合时,这是行不通的。而且,当您希望编译器能够将 fly 方法发送给对象时,您要做 一种 fly 方法。

Now, you could answer that Bird and Insect just happen to have fly methods and that's the end of the story. But that won't do when you need to speak of the set "all types that have a fly method". And you do need to speak of that set when you want the compiler to be able to send the fly method to an object on the grounds that it has a fly method.

解决方案是一种协议:

protocol Flier {
    func fly()
}

现在Flier是类型。可以在期望使用Flier的情况下使用符合Flier的任何类型的实例,并且编译器会让您告诉任何Flier fly ,因此问题得以解决:

Now Flier is a type. An instance of any type that conforms to Flier can be used where a Flier is expected, and the compiler will let you tell any Flier to fly, so the problem is solved:

var myFlier : Flier = Bird() // if Bird conforms to Flier
myFlier.fly() // legal

这篇关于何时在Swift中使用协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆