您当前的位置: 首页 >  swift

Swift对象实例方法名混淆的解决

发布时间:2016-09-06 17:12:49 ,浏览量:0

在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:)
关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    107766博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0751s