UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Use of structs

Forums > macOS

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.

3      

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?

You would not assign a separate variable to hold each Person object loaded in; you would read your JSON into an array of Person structs. Then, if you need a specific Person record, you would get it via some method on your array, like filter or something else.

To be honest, I'm not sure why you would need individually named variables for each Person in the first place. Why would you use a variable called johnsmith? What if you need to find a Person named Jane Doe or Rex Splode or any one of an infinite number of possible names? Would you have to create a janedoe or rexsplode variable for them? How would you account for names you never anticipated? This just makes no sense to me.

let people: [Person] = //load in your JSON Person objects somehow
let latestUser = people.filter { $0.username == "johnsmith" }

Or if you read your JSON into Core Data or Firebase or whatever, you could use their built-in query functions to get at specific records.

3      

You see? I am now smaking myself in the forehead... I don't know why this happens. I hate brain blocks! As always, thank you @roosterboy!

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.