swift – 如何创建引用特定函数的函数类型的类型

swift – 如何创建引用特定函数的函数类型的类型,第1张

概述斯威夫特的 official Documents说 You use function types just like any other types in Swift. For example, you can define a constant or variable to be of a function type and assign an appropriate function to t 斯威夫特的 official Documents说

You use function types just like any other types in Swift. For example,you can define a constant or variable to be of a function type and assign an appropriate function to that variable:

func addTwoInts(a: Int,_ b: Int) -> Int {    return a + b}var mathFunction: (Int,Int) -> Int = addTwoInts

这里是示例代码:

it define a variable called mathFunction,which has a type of a function that takes two Int values,and returns an Int values. Set this new variable to refer to the function called addTwoInts

问题:函数类型可以像Swift中的任何其他类型一样使用,我想知道,因为如何创建一个类型别名,它具有一个带有两个Int值的函数类型,并返回一个Int值.设置此新变量以引用名为addTwoInts的函数

我试过这个,显然,我错了.

解决方法 您无法将功能分配给类型.

您只能指定一种类型.

typealias mathFunctionType = (Int,Int) -> Intlet mathFunction: mathFunctionType = { int1,int2 in    return int1 + int2}
总结

以上是内存溢出为你收集整理的swift – 如何创建引用特定函数的函数类型的类型全部内容,希望文章能够帮你解决swift – 如何创建引用特定函数的函数类型的类型所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1008810.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存