So, as I've mentioned before I am moving over from Python to Swift for macOS.
One prevailing topic I keep seeing is to use data structures in Swift. In Python I used dictionaries exstensively to hold my data. It was easy to refer to that data, but that's because I am well versed in Python dictionaries, I guess.
However, I see a lot of posts stating that dictionary use in Swift isn't a good basis to build your code upon as you WILL run into issues later. I've not seen any posts with examples of those issues, but the sentiment exists nevertheless.
So, I thought "Okay, I'm moving to Swift, so Swiftly." But, I'm having a a difficult time understanding how to use structs. I get the general premise that it can keep your data organized, but it seems so only if you know what your variable names are that will store that data at build time.
For example:
struct Person {
firstName:String = ""
lastName:String = ""
username:String = ""
favoriteMovie:String = ""
age:Int = 0
}
johnsmith = Person()
johnsmith.firstName = "John"
johnsmith.lastName = "Smith"
johnsmith.userName = "johnsmith"
johnsmith.favoriteMovie = "Gone with the Wind"
johnsmith.age = 38
So this provides a data structure to store Person type information. It looks great and easy to use and I understand how to assign values to the person properties as shown.
However, if I was to read a Json file of 5,000 Persons, how would I assign each of those persons to a unique Person type, and be able to refer to that specific record?
As another example, this won't work because dynamic variable names aren't possible in Swift from what I've read: (jsonData.userName is equal to "johnsmith" in this example)
var jsonData.userName = Person(), or
var jsonData.userName.self = Person()
All the examples I see they already know there is a John Smith, so they assign it in the code as var johnsmith = Person(). But what if you don't know there is a John Smith? What if at program startup, or loading of Json data, there is a Johnny Appleseed? How would you go about assigning a unique variable to represent Johnny and his information and be able to refer to it throughout program execution?
I'm sure this is quite elementary, but I am getting a brain block and cannot figure this out. I will probably smack myself in the forehead as soon as someone posts a solution.
Thanks for your help.