swift中函数与泛函之间的差异 [英] Diffrence between Function and Generic Function in swift

查看:107
本文介绍了swift中函数与泛函之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道swift中函数和泛型函数之间的区别。以下
函数和泛型函数正在做同样的事情。谁能告诉我通用函数的确切用法?

i want to know the difference between the Function and Generic function in swift.Following Function and Generic function is doing same..can anyone tell me the exact use of Generic functions ?

func simpleMin<T: Comparable>(x: T, y: T) -> T {  //Generic functions
    if x < y {
        return y
    }
    return x
}


func sampleMin(x:AnyObject,y:AnyObject)->AnyObject{  //Function
    if x.integerValue < y.integerValue {
        return y
    }
    return x
}


推荐答案

通用函数允许您在函数的参数和结果上使用Swift的类型安全性来编写更安全,更清晰的代码。例如,你的第一个函数要求传入的两个参数都是相同的类型,并保证相同类型的返回值:

Generic functions let you use the type safety of Swift on both the parameters and the result of the function to write safer, cleaner code. For example, your first function requires that both parameters passed in be of the same type, and guarantees a return value of that same type:

let minInt: Int = simpleMin(5, 12)
let minDouble: Double = simpleMin(5.0, 12.0)

而你的第二个函数没有这样的要求而没有这样的保证:

whereas your second function makes no such requirements and no such guarantee:

let minOne: AnyObject = sampleMin(5, 12.0)     // minOne is an AnyObject holding an Int
let minTwo: AnyObject = sampleMin(5.0, 12)     // minTwo is an AnyObject holding an Double
let minThree: AnyObject = sampleMin("Five", true)   // what is supposed to happen here, exactly?

使用这些 AnyObject 结果我需要做额外的检查,以确保我理解返回的函数,因为 AnyObject (显然)比我的原始参数更具体。

With these AnyObject results I would need to do extra checks to make sure I understand what the function returned, since AnyObject is (obviously) much less specific than my original parameters.

此外,泛型函数允许您对它们接受的参数设置约束,因此您可以确保仅使用有意义的参数调用该函数。你的第一个函数要求参数符合 Comparable 协议,这意味着我不能只用两个随机对象来调用它。例如,编译器将让我用两个 UIView 实例调用你的第二个函数,并且在执行该代码时崩溃之前不会有任何问题。

Moreover, generic functions allow you to put constraints on the parameters they accept, so you can make sure that the function is called only with arguments that make sense. Your first function requires that the parameters conform to the Comparable protocol, meaning that I can't just call it with two random objects. The compiler will let me call your second function with two instances of UIView, for example, and there won't be any problem until the crash when that code is executed.

了解协议是使用泛型的重要部分。协议定义了一些在整个类中有用的特定功能,并将该功能的实现留给了类本身。上面的 Comparable 协议就是一个例子;这是定义:

Understanding protocols is an important part of using generics. A protocol defines some specific functionality that would be useful across a whole range of classes, and leaves the implementation of that functionality up to the classes themselves. The Comparable protocol above is one example; here's the definition:

protocol Comparable : Equatable {
    func <=(lhs: Self, rhs: Self) -> Bool
    func >=(lhs: Self, rhs: Self) -> Bool
    func >(lhs: Self, rhs: Self) -> Bool
}

这就是说任何符合<$ c的对象$ c> Comparable 协议将有使用< = > = > 运算符 - 也就是说,如果>你可以写 b 任何两个对象都是可比较

This is saying that any object that "conforms to" the Comparable protocol is going to have definitions for using the <=, >=, and > operators -- that is, you'd be able to write if a > b for any two objects that are both Comparable.

更多阅读:

Swift编程语言:泛型

通用编程 - 维基百科

泛型一直是C#和Java等功能的特性,因此您可以找到更多资源来帮助您您了解它们的好处以及如何在文档中使用它们。

Generics have long been a feature of C# and Java, among other languages, so you may find more resources to help you understand their benefits and how to use them in their documentation.

这篇关于swift中函数与泛函之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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