Der*_*328 5 polymorphism xcode closures ios swift
// Assume you have two functions.
// Swift considers these functions as distinct
func cars(left look: String) {}
func cars(right look: String) {}
// How can I store them in a variable for later use?
let c: (left : String) -> () = cars
let c2: (right : String) -> () = cars
Run Code Online (Sandbox Code Playgroud)
当我尝试将这些方法存储到变量中时,收到一条错误消息,指出“汽车”不明确。我可以做什么来区分它们?
您必须帮助编译器决定您想要哪个函数,因为在决定函数的签名是否对赋值有效时,它不区分参数名称。
您可以通过具体参数名称来消除您的意图的歧义
let c: (left :String) -> () = cars(left:)
let c2: (right:String) -> () = cars(right:)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9412 次 |
| 最近记录: |