Map text as a class

Is it possible to convert a file containing only swift classes into a real class. And the class in the text file.
And the classes in the text follow a protocol, and this protocol exists in the running swift

For example, I have A.swift and B.swift, A.swift has a protocol AP, B.swift has a class BC, and BC follows the AP protocol. Now I run A.swift, in which I want to use the B.swift class in the program.

If this can be achieved, swift can be used for data transmission like json.

Ultimately all data transmission is happening in bytes.

You need to think about:

  1. What operations do want to do on your data when it is loaded in program memory.
  2. How to model in-memory representation of your data.
  3. How to structure serialized representation.
  4. How to convert between in-memory and serialized representations.

Looks like you want your serialized representation to be Swift source code. Swift source code can contain a lot things, including imports of frameworks, declarations of custom operators, conditional compilation directives, and arbitrary executable code which pose security risks. Is this really the best representation of your data?

Note that JSON is inspired by JavaScript, but normally JSON is not parsed by feeding it into full-powered JS interpreter. For several reasons, but for security reasons in particular. If you have some specific requirements that JSON does not satisfy (maybe native representation of dates), you could design your own data format and take some inspiration from Swift. But it would a new thing, completely different from the Swift programming language.

Terms of Service

Privacy Policy

Cookie Policy