Giter Club home page Giter Club logo

countryfacts's Introduction

Country Facts

An app that contains facts about countries: show a list of country names in a table view, then when one is tapped bring in a new screen that contains its capital city, size, population, currency, etc

day60.gif

Day #59 - Challenge #5 - www.HackingWithSwift.com/100/59

Your challenge is to make an app that contains facts about countries: show a list of country names in a table view, then when one is tapped bring in a new screen that contains its capital city, size, population, currency, and any other facts that interest you. The type of facts you include is down to you – Wikipedia has a huge selection to choose from.

To make this app, I would recommend you blend parts of project 1 project 7. That means showing the country names in a table view, then showing the detailed information in a second table view.

How you load data into the app is going to be an interesting problem for you to solve. I suggested project 7 above because a sensible approach would be to create a JSON file with your facts in, then load that in using contentsOf and parse it using Codable. Regardless of how you end up solving this, I suggest you don’t just hard-code it into the app – i.e., typing all the facts manually into your Swift code. You’re better than that!

Day 60

JSON Source

I used the World Factbook by CIA data from https://github.com/factbook/factbook.json

Codable data structure

The JSON structure is composed from multiple objects nested in one another, eg:

{
  "Geography": {
    "Location": {
      "text": "Southwestern Europe, Pyrenees mountains, on the border between France and Spain"
    },

This can be represented with structs which contain structs and so on:

struct Geography: Codable {
    struct Location: Codable {
        let text: String
    }
    let Location: Location
}

Another issue was that some of the object names contained spaced. To implement this, a CodingKeys enum needs to be defined as well:

struct Country: Codable {
    
    enum CodingKeys: String, CodingKey {
        case people = "People and Society"
        case introduction = "Introduction"
        case geography = "Geography"
        case government = "Government"
    }

All objects have to be included in the enum though.

Loading from the Bundle

The .json files are stored in the Bundle, so the files can be read using:

FileManager.default.contentsOfDirectory(atPath: Bundle.main.bundlePath)

And checked for the .json suffix.

DetailViewController

I’ve used the Storyboard to define the DetailViewController. So when a row is selected, I instantiate the DetailViewController using its identifier, casting it as DetailViewController in order to assign the corresponding country struct.

guard let detailVC = storyboard?.instantiateViewController(identifier: "DetailID") as? DetailViewController else { return }
        
detailVC.country = countries[indexPath.row]
        
navigationController?.pushViewController(detailVC, animated: true)

UIScrollView & UITextView & UILabel

Setting up the Scroll View to work well with a UITextView was pretty painful. I had to set up another “Content” View inside the Scroll View and have the height of the scroll view modify dynamically based on the height of the Text View. In the end I opted for a UILabel instead, and I could remove the extra Content View.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.