Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How to give an associated value an unchangeable value? (Swift)

Hi, so here my code:

          enum iPhone {
    case v7(type: String, color: String, storage: Int)
    case v6(type: String, color: String, storage: Int)
    case vSE(color: String, storage: Int)

    func sell() -> iPhone {
        switch self {
        case .v7(let type, let color, let storage): 
            return .v7(type: type, color: color, storage: storage)

        case .v6(let type, let color, let storage):
        return .v6(type: type, color: color, storage: storage)

        case .vSE(let color, let storage):
        return .vSE(color: color, storage: 128)
        }
    }
}

Let's say that from now on, Apple will sell iPhone SE just with 128GB storage. Therefore there's no point in giving us an option to manually type the storage. I'm able to always return iPhone SE with 128GB, but I haven't figured out how to do so without having to manually type the storage. Any idea how to do it?

1 Answer

Hi Jiri,

An associated-value for an enum case is already a constant so I'm guessing that what you're really asking is if you can assign default values to associated values... you can't. But, you don't really need to either; the way you have it works just fine. Any particular reason why you don't want to hardcode the 128? Consider the fact that even if you abstracted that part out, you'd still — at some point — have to hardcode it in... or load it from persistence.

Hi Mikis,

thanks for an answer. I'm not sure whether we understand each other completely so I'll try to explain it once again (and hopefully, it will be more clear what I mean)

I want iPhone SE to be only in 128GB option and I have achieved this with this line:

return .vSE(color: color, storage: 128)

However, because of this line

case .vSE(let color, let storage):

I always have to set the storage (even though it's meaningless). Let's say I want to sell Gold Rose iPhone SE

let roseGoldSE = iPhone.vSE(color: "Rose Gold")
roseGoldSE.sell()

And this won't work as I have to assing a (random) value to storage. So I will now assign a random value to the storage

let roseGoldSE = iPhone.vSE(color: "Rose Gold", storage: 1)
roseGoldSE.sell()

I mean,sure, it works and I just have to type in one more symbol, however it's neither clever or elegant :)

Right. I understand what you're trying to do... I just am having a hard time imagining a use-case where that would come in useful. That feature — i.e., default values for enum's associated-values — is not available in Swift. The use-case you cite — regarding the rose-gold iPhone — is inconvenient... but in a very trivial sense. Assuming you're okay with hardcoding values for the other enum cases, like this:

let iPhone7 = .v7(type: "7", color: "SpaceGray", storage:128)

then I don't see how it's so different to do this:

let iPhoneSE = .vSE(color: "Rose Gold", storage: 128)

There's no need to hardcode a random value, after all. This way also leaves your code a bit more flexible for when Apple releases a larger storage number for the SE. Nothing wrong with some additional flexibility, especially when its cost is so trivial.