xcode 需要方法参数的类型和协议

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24231498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:05:52  来源:igfitidea点击:

Require type and protocol for method parameter

iosxcodecocoaprotocolsswift

提问by cschwarz

I am playing around with Swift and am stumbling over the following problem: given I have the predefined class Animal:

我正在玩 Swift 并遇到以下问题:鉴于我有预定义的类Animal

//Predefined classes
class Animal {
    var height: Float = 0.0
}

I now write the class Zoowith the constructor accepting animals. But the Zoowants every animal to have a name and hence defines the Namableprotocol.

我现在Zoo用接受动物的构造函数编写类。但是他们Zoo希望每只动物都有一个名字,因此定义了Namable协议。

protocol Namable {
    var name: String {get}
}

class Zoo {
    var animals: Animal[] = [];
}

How would you write an addAnimalmethod that requires the object being passed as parameter to be both of typeAnimaland conform to protocolNamable? And how do you declare that for the animalsarray?

您将如何编写一个addAnimal方法,要求作为参数传递的对象既是类型Animal符合协议Namable?你如何为animals数组声明它?

    func addAnimal:(animal: ????) { ... }

In Objective-C, I'd write something like this

在Objective-C中,我会写这样的东西

    - (void)addAnimal:(Animal<Namable>*)animal {...}

回答by Kevin

You can use a generic with a whereclause with multiple conditions.

您可以使用where具有多个条件的子句的泛型。

func addAnimal<T: Animal where T: Nameable>(animal: T) { ... }

Amendment: You should probably make the whole class this generic so you can type the array properly

修正:您可能应该将整个类设为通用,以便您可以正确键入数组

class Zoo<T: Animal where T: Nameable> {
    var animals : T[] = []
    func addAnimal(a: T) {
        ...
    }
}

回答by Sulthan

To me, this seems more an architecture problem:

对我来说,这似乎更像是一个架构问题:

Nameableis strange as a protocol. Logically, every Animal has the ability to be named so Animalshould always conform to Nameable

Nameable作为协议很奇怪。从逻辑上讲,每个动物都有被命名的能力,因此Animal应该始终符合Nameable

It would be much simpler to allow for nilnames when the animal is not named instead of having animals that can have a name and animals that can't.

nil当动物没有被命名而不是让动物有名字而动物没有名字时,允许命名会简单得多。

Then you can enforce names in Zoosimply by an assert.

然后你可以Zoo简单地通过一个assert.

回答by Philipp Otto

Swift 3 version

斯威夫特 3 版本

In Swift 3 the structure changed a bit. That′s why you will get a deprecation warning for the old structure. Here′s the new one:

在 Swift 3 中,结构发生了一些变化。这就是为什么您会收到旧结构的弃用警告的原因。这是新的:

For functions the expected protocols are located after the parameters definition of the function. Example:

对于函数,预期的协议位于函数的参数定义之后。例子:

func addAnimal<T: Animal>(animal: T) where T: Nameable

For enums or classes the structure also changed

对于枚举或类,结构也发生了变化

enum ZooEnum<T: Animal> where T: Nameable 

class Zoo<T: Animal> where T: Nameable 

回答by Binarian

<>is in Objective-C: conforms to protocol

<>在 Objective-C 中:符合协议

<>is in Swift for generics

<>在 Swift 中用于泛型

You can do more with the wherekeyword

您可以使用where关键字做更多事情

Use whereafter the type name to specify a list of requirements—for example, to require the type to implement a protocol, to require two types to be the same, or to require a class to have a particular superclass.

在类型名称后使用where来指定要求列表——例如,要求该类型实现协议、要求两个类型相同或要求一个类具有特定的超类。

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.

摘自:Apple Inc. “The Swift Programming Language”。电子书