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 Build a Vending Machine App in Swift Loading Data From a Resource Finishing Touches

imelda cloutier
PLUS
imelda cloutier
Courses Plus Student 2,597 Points

this line of code brings an error for me

guard let selection = VendingSelection(rawValue: key) else {

The Hacker
The Hacker
20,260 Points

Hello Imelda,

Could you please paste all the code, so we can figure out what's happening?

Thanks

Isaac Cruz

2 Answers

Xavier D
PLUS
Xavier D
Courses Plus Student 5,840 Points

Hi Imelda,

If that initializer with the rawValue parameter came up in Xcode's autocomplete on the VendingSelection enum example, then that means you must have specified a type on the enum...

...Perhaps you did it like this?

enum VendingSelection: String {
    case soda, dietSoda, chips, cookie, sandwich, wrap, candyBar, popTart, water, fruitJuice, sportsDrink, gum
}

I'm assuming you did it that way because of the argument provided (i.e. key). If you did it that way, then you're missing the quotation marks around the argument. If you add the quotation marks, you should get nil back since the initial example did not contain that raw value. However, if you modified the enum with that raw value, then you should get back an optional value, and I guess it would make sense for you to use guard and unwrap it.

Xavier D
Xavier D
Courses Plus Student 5,840 Points

Hi Imelda,

Sorry, I answered prior to watching the video. It seems that Pasan did end up specifying a type for the enum as String, using the local constant key containing an optional string...

If that's the case, then you wouldn't need the quotation marks since the constant contains an optional string...

Other than that, I don't see any errors in that line of code.

Xavier D
Xavier D
Courses Plus Student 5,840 Points

Hi Imelda,

Hey, I just got an "Use of unresolved identifier 'key' error"... I was wondering why I got that...why Xcode believes key doesn't exist. But then I noticed that I had the guard let statement outside for loop and scoped to the method boy, thus in that sense, key doesn't exist since it's a local constant of the loop. Realizing the error is due to a misplacement of a ending curly brace or two, it went away once I put the guard let within the same scope as the key. However, an new error appeared with VendingSelection cannot be constructed because it has no accessible initializers. But enums come with that initializer...then I realized that I didn't specify the enum type... I fixed it by doing this.

enum VendingSelection: String {
    case soda, dietSoda, chips, cookie, sandwich, wrap, candyBar, popTart, water, fruitJuice, sportsDrink, gum
}

Now I just have a bunch of yellow issues due to a bunch of constant/variables not being used...

Hope that helped.

XD

imelda cloutier
PLUS
imelda cloutier
Courses Plus Student 2,597 Points

lost all my data. restarting from point 0. I ll repost if it happens again. from what I remember type was specified, they had raw value.

Xavier D
Xavier D
Courses Plus Student 5,840 Points

Hey Imelda,

Sorry about that. What I started doing (along with my backup), I started using my free cloud data that Apple gives you with your Apple ID. Therefore, if something like a fatal error occurs and you lose data, everything is safe on the cloud. In addition, I can access cloud data on any mac profile name that is linked to the id. I suggest that if you have an iPad, download Playgrounds, because not only you can write code with it, you can play coding games game too (fun), and iCloud backs up the data in a Playgrounds folder. Careful though, the games are kinda heavy on the data, and can take up all of your iCloud space.