LAST CHANCE: Save 50% on all my Swift books and bundles! >>

MoonShot 4 /11?

Forums > 100 Days of SwiftUI

Was there a specific reason there were two structs in this lesson for name and address? Why not just one? And why inherit address from Address?....These two....

struct User: Codable {
    let name: String
    let address: Address
}

struct Address: Codable {
    let street: String
    let city: String
}

2      

There is no inheritance here. What there is, is a property called address that is of type Address.

As to why separate out Address into its own type, think about it: An address is a discrete thing unto itself. A person doesn't have a street and a city and a postal code; a person has an address, which itself consists of a street, city, etc. So Address should be its own data type that you can then assign to the address property of a User.

2      

@rooster gives great insight:

As to why separate out Address into its own type, think about it: An address is a discrete thing unto itself

Also consider your user might have more than ONE address. They may have a shipping address--send my cool HackingWithSwift swag here. And another address for billing--send the invoice to my personal secretary who handles such administrivia.

This is an EXCELLENT example of why you might consider breaking your data models into smaller discrete objects.

2      

I'm not sure of this one, can you explain in further detail? "What there is, is a property called address that is of type Address."

Also consider your user might have more than ONE address. They may have a shipping address--send my cool HackingWithSwift swag here. And another address for billing--send the invoice to my personal secretary who handles such administrivia.

How do you do that? Or how can you add another address in the JSON file and relate it to the same user? The JSON file doesn't look dynamic.

2      

How do you do that?

Try this in a Playground...

struct User: Codable {
    let name:                String
    let usersHomeAddress:    Address // <-- this defines the property usersHomeAddress is a TYPE of Address
    let usersBillingAddress: Address // <-- this defines the property usersBillingAddress is the same type, but different content
}

let homeAddress    = Address(street: "Hufflepuff House Lane", city: "Hogwarts") // define FIRST address
let billingAddress = Address(street: "10 Downing Street",     city: "London"  ) // define a SECOND address

// Create a new user and give that user object TWO addresses
let george = User(name: "George", usersHomeAddress: homeAddress, usersBillingAddress: billingAddress)

george.name                        // <-- returns George
george.usersHomeAddress.city       // <-- returns Hogwarts
george.usersBillingAddress.street  // <-- returns 10 Downing Street

2      

I'm not sure of this one, can you explain in further detail? "What there is, is a property called address that is of type Address."

This is an example of inheritance:

class Address {
    let street: String
    let city: String
    let state: String
    let postal: String
}

class BusinessAddress: Address {
    let zone: String
}

class HomeAddress: Address {
    let subdivision: String
    let nearestSchool: CLLocationCoordinate2D
}

We have a type called Address and then we have two other types—BusinessAddress and HomeAddress—that inherit from Address. Because they inherit from Address, they contain the same properties (and methods if I had defined any) that Address does, plus anything new defined in their own declarations.

In your original example:

struct User: Codable {
    let name: String
    let address: Address
}

the type User has a property that is of type Address. Nothing is being inherited from anything else. You simply have one type with two properties, each of which has its own type.

2      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

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

Reply to this topic…

You need to create an account or log in to reply.

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.