Post

Replies

Boosts

Views

Activity

Reply to Is it possible to serialize/deserialize closures in swift?
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(); } }
Topic: Programming Languages SubTopic: Swift Tags:
Mar ’23