私はクロージャを処理するメソッドを作成したいと思います。クロージャがメソッドの呼び出しが含まれていますし、私の閉鎖方法は、順番にそれらを実行する必要があり、例えば:このクローズアップを書くには?
when("I tap the Get Coffee button")
{
_ in
self.tap(p.button1)
self.wait(1)
self.tap(p.button1)
return true
}
そして、私の(簡体字)の閉鎖方法:
:これはエラーになりcannot convert value of type '(_) -> _' to expected argument type '(() -> Bool)?'
public func when(_ name:String, closure:(() -> Bool)? = nil)
{
if let c = closure
{
_ = c()
}
}
これが機能するには、closure引数でどの型を定義する必要があるのか分かりません。
また、私はそれがで動作するように、閉鎖中self.
参照を排除したいと思います:
when("I tap the Get Coffee button")
{
_ in
tap(p.button1)
wait(1)
tap(p.button1)
return true
}
閉鎖はオプション作るのポイントは何ですか?クロージャを使わずに 'when'メソッドを呼び出すことはありますか? – rmaddy
なぜ、戻り値を無視するだけの場合は、戻り値の型が 'Bool'のクロージャを定義するのですか? – rmaddy
@maddy両方の理由があるので、私は書いた(簡略化した)。 – BadmintonCat