Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swift泛型 #3

Open
miss-shiyi opened this issue Sep 29, 2021 · 0 comments
Open

swift泛型 #3

miss-shiyi opened this issue Sep 29, 2021 · 0 comments
Assignees
Labels
Swift Extra attention is needed

Comments

@miss-shiyi
Copy link
Owner

1. 泛型

泛型:能够在我们定义的函数或方法中编写足够灵活、可重用性强的功能,并可以与任何类型的参数按照我们定义的要求进行工作。使用泛型,我们可以写出避免重复和以更抽象的方式明确表达自己意图的代码。 泛型Swift中最强大的功能之一,在Swift的标准库里,很多都是使用泛型构建的。事实上,我们已经使用了很多基于泛型的库,只是自己没有察觉而已。例如,数组和字典类型就是泛型集合。我们既可以创建基于“Int”类型的数组,也可以创建基于“String”类型的数据。同样,我们可以为字典的值存储各种自己所需的类型值。

func chgangeTwodatas<T>(oneData: inout T , anotherData: inout T){
    print("转换之前的数据oneData=\(oneData),anotherData=\(anotherData)")
    let temp = oneData
    oneData = anotherData
    anotherData = temp
    
    print("交换之后的数据 oneData=\(oneData),anotherData=\(anotherData)")
    
}

var a3 = "mike"
var a4 = "jack"

chgangeTwodatas(oneData: &a3, anotherData: &a4)

类型参数:指定命名一个占位符,使用一对尖括号括起来“”,并且紧跟在函数后面,如“泛型转换函数”。

@miss-shiyi miss-shiyi added the Swift Extra attention is needed label Sep 29, 2021
@miss-shiyi miss-shiyi self-assigned this Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Swift Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant