小编Dee*_*kal的帖子

如何将类作为参数传递给Swift中的函数?

让我们考虑一下我有两个不同的类.

class A {
var something = "Hello"
}

class B {
var something = "World"
}
Run Code Online (Sandbox Code Playgroud)

现在

class C {

func request() {

    //Call with class A or B it can contain any class. I can call either class A or B depending on condition
    update(myClass: A or B)
}

func update(myClass:A or B ) {
    print(myClass.something) //Since both class have same varaible var something so this code should work either i pass class A or B through function …
Run Code Online (Sandbox Code Playgroud)

swift

1
推荐指数
1
解决办法
733
查看次数

标签 统计

swift ×1