简体   繁体   English

快速转义闭包以及如何在Java中执行

[英]Escaping closure in swift and how to perform it in Java

I'm developing an app for both iOS and Java and in my swift program I have a function that uses an escaping closure that passes an array of strings. 我正在为iOS和Java开发一个应用程序,在我的快捷程序中,我有一个函数使用转义的闭包传递字符串。 In swift, it's func definition goes as 很快,它的功能定义如下

func foo(uid: String, onComplete: @escaping([String]) -> Void { 
    onComplete(someStringArray)
}

and its func call is 它的函数调用是

class.foo(uid: player1, onComplete { (stringArray) in {
    do something with stringArray
}

My main focus is now figuring that out for Java. 我现在的主要重点是为Java弄清楚这一点。 The BIG issue is I'm using Google's Firebase Database which loads data asynchronously so simply making a return function won't work since the return will be called before all the data is loaded. BIG的问题是我使用的是Google的Firebase数据库,该数据库异步加载数据,因此仅使return函数起作用是不可行的,因为return将在加载所有数据之前被调用。 I've heard of maybe call backs, but I figured I'd ask here first before I spend precious hours on the wrong task. 我听说过可能会回电话,但我想我要先在这里问一下,然后再将宝贵的时间花在执行错误的任务上。 I'm thinking the Java version would be 我在想Java版本是

void foo(String uid, onComplete...) { 
onComplete(someStringArray)
}

Anyways I hope I was as detailed as possible and appreciate any help at all! 无论如何,我希望我尽可能详细,并感谢您的帮助! -Ben -ben

You can use Lambda Expresions expressions. 您可以使用Lambda Expresions表达式。

Forgive my function naming, but here is an example: 原谅我的函数命名,但这是一个示例:

public class LambdaExample{

    interface Description {
        void describeThis(String str);
    }

    public static void getDescription(Description description, String str){
        description.describeThis(str);
    }

    public static void main(String []args){
        getDescription(new Description() {
            @Override
            public void describeThis(String str) {
                System.out.println(str + " is handsome");  // Meow2x is handsome
            }
        }, "Meow2x");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM