global variables in swift and saving a struct in a file

Hi,

I have to translate in swift a finite element for buildings software writen in delphi before.

In this software, there are many global variables (structs, arrays, matrices, enums ...

Do I put them in a isolated swift file as we do for classes or in the main file ?

The second part of my question is more important.

Each building created will have its data in a struct and I would like to save it in a file.

How can I do that ?


Thanks in advance for answers,


Best regards,

Alfred

Answered by Claude31 in 329516022

Is it OSX or IOS App ?


Probably the first question is to analyze if you really need so many globals.


For enum definition, struct definition, no problem. And it may be good to have them in a specific Swift file.


But for var, you should consider whether they should be part of the model associated to an instance of the computation class ; globals bring the risk to be modified by several objects.


For the second question, depending on how complex data model is, you may have to consider a database structure, such as Core Data. Or save data as separate files.

Accepted Answer

Is it OSX or IOS App ?


Probably the first question is to analyze if you really need so many globals.


For enum definition, struct definition, no problem. And it may be good to have them in a specific Swift file.


But for var, you should consider whether they should be part of the model associated to an instance of the computation class ; globals bring the risk to be modified by several objects.


For the second question, depending on how complex data model is, you may have to consider a database structure, such as Core Data. Or save data as separate files.

Do I put them in a isolated swift file as we do for classes or in the main file ?


As you like. Source file structure of Swift projects is free. You can put them in isolated files (not a single file), or like classes or in the main file.

(Though, it is not clear what is the main file in Swift project.)


But, generally, you should better mimic the source file structure of the original project (in Dephi?), when translating foreign projects.


Each building created will have its data in a struct and I would like to save it in a file.

How can I do that ?


Depending on the data representing the struct and how your app use it. Better show a concrete example.

Thank you a lot for your verygood answer.

The data for a building or bridge is a struct type with inside other structs, different vars strings, int, doubles, arrays of structures. The size of this structure is about 12K (in my delphi environnement in windows.

I don't want to put it in a database because I am no more working, I am retired and I will not executing the program with too many customers and buildings.

I think to write and read the building in a file with bytes (initial byte of the struct and copy the struct length of bytes, but I don't know how to do that in swift. I am very new in this language.


Thank you again,


Best regards,


Alfred

global variables in swift and saving a struct in a file
 
 
Q