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

SOLVED: Days 60: Consolidation - challenge completed (and a simple question)

Forums > 100 Days of SwiftUI

Hi,

I managed to finish the challenge of day 60, it works well so I share if it interests someone to compare his method to mine and discuss it. https://drive.google.com/file/d/1T5RPMOhhUkwnzKtSVRyy5SQ5BnVA58aC/view?usp=sharing Tomorrow, I have to add core data to it, so I share before 🙂

Just a question if anyone can enlighten me.

I just wonder why this :

class Someone: Decodable {
    let id: Int
    let name: String
    let age: Int
}

does not cause any error when there is no initialization ?

For my decodedObject in the challenge i used a struct, and not a class (everything works find), maybe i should do the challenge with a class, just to see if i will get there too.

3      

Hi Maxime,

The class works because it inherits the default initializer from the Decodable protocol. If you remove the protocol, you will get the error Class 'Someone' has no initializers.

To be honest, I also used struct and not class. In my opinion you should always work with structs whereever possible and only with classes when it is really needed... And in this project, where we do not edit data and need multiple views share the same object (=identity), I stick with the struct.

Regards Philipp

6      

Hi Philipp,

great, thank you! I thought it was something like that, thank you for this precision. I didn´t know that a class inherited from the initialization of its protocol, it's good to know.

3      

o thank u man I'm int he same challenge and I don't know how to start, well i have an idea, but it doesn't work :(

3      

I've put up my version of the challenge on GitHub: https://github.com/pd95/HWS-FriendFace/commits/master

Checkout the initial commit which is the state after day 60 has been implemented. I used:

struct User: Identifiable, Codable {
    let id: UUID
    let isActive: Bool
    let name: String
    let age: Int
    let company: String
    let email: String
    let address: String
    let about: String
    let registered: Date
    let tags: [String]

    struct Friend: Identifiable, Codable {
        let id: UUID
        let name: String
    }

    let friends: [Friend]
}

to decode the file.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.