DEV Community

Oluwasanmi Aderibigbe
Oluwasanmi Aderibigbe

Posted on

3 3

Day 39 of 100 Days Of SwiftUI

I just completed day 39 of 100 days of SwiftUI. Today I learnt about navigation links and also how to parse JSON using hierarchical Codables.

Navigation links are used in Swift to show new screens. You can also use the sheet modifier to show a new screen but the sheet modifier is more suited to showing unrelated content like settings while navigation links should be used for showing details about a user's selection.

The Codable protocol is used to turn JSON into Swift objects. It works fine for simple JSON structures. But for more complicated ones you will have to help Swift out.

let input = """
    {
        "name": "Sanmi",
        "about": {
            "level": "9001",
            "occupation": "Android Developer"
        }
    }
    """
struct User: Codable {
    var name: String
    var about: About
}

struct About: Codable {
    var level: String
    var occupation: String
}
let data = Data(input.utf8)
let decoder = JSONDecoder()
if let user = try? decoder.decode(User.self, from: data) {
    print(user)
}
Enter fullscreen mode Exit fullscreen mode

This code converts a JSON string to a Swift object. It's actually pretty simple. All you have to do is create structs that match the JSON

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay