Is it possible to serialize/deserialize closures in swift/iOS?
Is it possible to serialize/deserialize closures in swift?
What do you mean exactly ?
Do you mean Multiple trailing closures ?
https://github.com/apple/swift-evolution/blob/main/proposals/0279-multiple-trailing-closures.md
They are available since Swift 5.3
Looking for swift equivalent of the below code snippet @FunctionalInterface public interface MySimpleFunctionalInterface extends Serializable { public String doSomething(int i); } —------------- //serialize the lambada public class SerailizeSimpleFI { public static void main(String[] args) throws IOException { FileOutputStream fileOutputStream = new FileOutputStream("serialized.txt"); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
MySimpleFunctionalInterface fi = (int i) -> {
System.out.println("incoming=" + i);
return String.valueOf(i);
};
objectOutputStream.writeObject(fi);
objectOutputStream.flush();
objectOutputStream.close();
}
}
—------------------
public class MySimpleFIUser {
private MySimpleFunctionalInterface fi;
public void setFi(MySimpleFunctionalInterface fi) {
this.fi = fi;
}
public void use()
{
int input=1;
if(fi!=null)
{
String res = fi.doSomething(input);
System.out.println("input="+input+",result="+res);
}
}
} —-------- public class Check { public static void main(String[] args) throws IOException, ClassNotFoundException {
MySimpleFIUser fiUser = new MySimpleFIUser();
FileInputStream fileInputStream = new FileInputStream("serialized.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
//deserialize the lambada and use it MySimpleFunctionalInterface fi = (MySimpleFunctionalInterface) objectInputStream.readObject(); objectInputStream.close(); fiUser.setFi(fi); fiUser.use(); } }
//serialize the lambada //deserialize the lambada and use it
Are you referring to the Java technique of serializing lambdas, like what’s described at https://www.baeldung.com/java-serialize-lambda ?
Swift has no equivalent feature.