Swift对象实例方法名混淆的解决
发布时间:2020-12-14 06:46:21 所属栏目:百科 来源:网络整理
导读:在Xcode7.x中,比如有以下一个类: class Foo { func test (v:Int,before:Int) - Int{ return v + 1 }} 我可以直接这么做: let foo = Foo () let f = foo.test f ( 11 ,before: 121 ) 但是如果Foo中有一个类似的方法呢? func test (v:Int,after:Int) - Int{ re
在Xcode7.x中,比如有以下一个类: class Foo{
func test(v:Int,before:Int)->Int{
return v + 1
}
}
我可以直接这么做: let foo = Foo()
let f = foo.test
f(11,before: 121)
但是如果Foo中有一个类似的方法呢? func test(v:Int,after:Int)->Int{
return v + 100
}
此时仅仅是Foo.test的赋值操作就会发生问题: Playground execution failed: 71.playground:8:9: error: ambiguous use of 'test(_:before:)'
let f = foo.test
编译器告诉你上下文是模糊不清的,因为你不知道要用哪个test! 此时我们可以明确写出我们想要的方法: let f = foo.test(_:after:)
let f2 = foo.test(_:before:)
f(11,after: 0) f2(11,before: 0)
但是在Xcode8beta中,_已不被允许,你必须这样写完整: let f = foo.test(v:before:)
let f2 = foo.test(v:after:)
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |