|
Hi Everyone, I'm working on a project which tells you about the country you are in. I get the users location from a placemark, which is then turned into a string (e.g. "Australia). This is the code for that
I also have a large JSON file with information about each country, with the name of the country also saved as a string ("Australia"). Here is what the JSON looks like.
From this, I want to be able to select the user's current country from the JSON file and display it on the screen (e.g. "Australia's Population is 25 Million"). Is there a way to do this with my current setup, or do any of you have an idea of another way I could do this? I'm very new to swift and swiftui, so I'm unsure as to how I should be doing this. Any help would be incredible. Thanks in advance. |
|
Hi, you could reorganize your JSON, so it would be like a dictionary, when you could use the country name as a key and get the other information. But I think the issue with current setup is that |
|
Do you need the country where the user is in a precise moment, or depending by where it resides? I would add a Locale for every country in the Json file and retrieve the data based on the locale of the user rather then the placemark (so language doesn't affect you either), bit it depends by what you want to achieve. |
|
In the engineering world there's a saying that the only sure way to eat an entire elephant is to finish one bite at a time. So much of structuring a program is breaking down your program's objectives into solvable "bite sized" pieces. You may be getting caught up in the enormity of your goals. ("I want to select the user's location from a placemark and grab the json data....") Break your problem into smaller pieces. Then solve one problem at a time. It's not JSONFor example: Don't think of your data as being in a JSON file. The data exists as JSON for only a short time. In your application, all your data could (should?) be stored in an array! Your first solvable puzzle is how to get the data from your application's bundle into an array. This should be easy to solve because @twostraws has some great(!) videos about loading JSON files from your application bundle. Which concepts do you have trouble with? Let us know which videos you understand, and which concepts cause headaches. Now you don't need to think about JSON anymore. Stop referring to JSON data. You have an array of countryInfo, you just have to find the entry in the array probably using a key (such as an ISO code) as suggested by @nemecek. What's the next bite of this elephant? Where are you?Getting the user's location. Solve that problem next. Are you a fan of CoreLocation? Countries are quite big. Do you need location down to the nearest square meter, or would you be comfortable with an approximation? Are you allowing a user to tap a location in MapKit? Again, find a smaller problem you can solve. Define a structure to define where your user is. Populate that structure with Latitude and Longitude, City Name, or Country name using Apple's built-in capabilities. Again, HackingWithSwift can help you. @twostraws created some videos in 2019 demonstrating MapKit and getting locations. Follow those tutorials. Take lots of notes. Play with MapKit in Playgrounds. If you have questions on the techniques, or parts of the videos, we are here to help you answer them. But please review those videos a few times before giving up! If you've solved a few smaller problems, you'll have a ready source of country data. You'll have the user's location (current or planned). The last few steps will be for you to link the two together and display a nice summary for your user. |
|
@Obelix thanks for your answer. I've managed to get all the way up to the last step, but this is where I'm unsure on what I should be doing. I have the data stored in an array, and I've got the ISO code from the users location loaded as a string, but I'm unsure as to how I should be linking them together. do I just make a huge if statement, or is there a better way of doing this? Thanks for your help so far, I really appreciate it. |
|
@souponastick says:
Excellent! THIS is why this forum exists! I love seeing how other help in with different points of views. You say you have the data stored in an array. Please share the structure of the array. Specifically, we'll need to know what data elements you're storing in your structure. Very important to think of your design details. What makes one array element different from the others? Probably the ISO code, but let's have a look at your structure just the same. Is your structure Identifiable? Others might just dish out the answer. But I enjoy @twostraws' methods of asking questions, providing some hints, and hoping in the end you become a better developer by finding the answer. Let's see your structure to find a path to raise you to the next level. |
|
@Obelix Hi, here is the code I'm using to unpack the json file.
In addition, here is the new structure of the json file, following the changes suggested earlier.
where do I go from here? |
|
Keep in mind, SwiftUI views are declarative. Just tell the view what you want it to show. For example, you have a long list of countries in a convenient storage package I'm calling fabulousCountries. So if I were to interview you, and asked what do you want to show in your view, you might say,
We'd then try to code something like:
So how do you construct a fun fact? Try this in a Playground.
Above you noted and asked:
Hope you can see that "NO!" you do not want a huge IF statement! Having your data in array gives you access to many array functions making it trivial to search through arrays for a single entry. |
|
SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.