Learn how to add a selection methods to the menu. Show the selection using the assessmentStatus function of playground support.
- [Instructor] A menu table like this is made for choosing an item. You can use the didSelect method of the table view to select an item. But, there's no easy way to send feedback to the user about their choice. You could make an alert if you wanted to do a lot of coding, but that's a little bit more work than I want. Playground Support has another solution: The AssessmentStatus Enumeration. There are two assessment status types, pass and fail. Pass will give you a message. Fail will also give you a message and a hint to solve the problem. They are meant for evaluation in education settings, but pass works well for a short alert while prototyping.
In our code, get ride of the live view and clean up your code just a little bit and I'm going to remove some of these. And then after the self or row at index path, I'm going to type "didsel" and you'll see the table view didSelectRowAt indexPath method and I'm going to go and hit that. And you'll see, of course, there's a little dot there.
That, again, is for overrides so go ahead and fix the override first. And then we're going to start putting some values in here. First thing we're going to do is let row=indexPath.row 'cause I just need the row here. So I'm going to go hit "let row = indexPath.row. This will make getting the row easier. Add the path assessment status like this: do a "PlaygroundPage.current.assessmentStatus" and that will equal ".pass(message:"You selected a \(menuitems[row]) at \(prices[row])") and that one you're just going to type out.
This will print a message giving us the pizza and the price. Run this and try a few pizzas. I'll pick the Huli Chicken and it says that it's $15.75. I'll check the Big Island, $21.95. Sausage, $11.95. Pepperoni, $10.95. And you can hit the "x" if you want it to go away. While pass works well for quick alert, fail adds a hint button which can contain an array of strings as hints for a right solution or the solution itself.
So in the code, we're going to now put this together so that we're going to have a row equal to zero. So if we get a sausage pizza order, we're going to upsell it to a Big Island pizza. And so what we're going to do here for this is I'm going to still need the row so I'm going to do it after the row and I'm going to throw an "if" in there and the condition will be "row=0" and then the code, if it's that, will be a current assessment status fail.
So we'll do "PlaygroundPage.current.assessmentStatus = and this time I'm going to do "PlaygroundPage.assessmentStatus.fail". And here I have hints that are all those different kinds of hints that I can have. I'm only going to use one and it's going to be a values of "Try an Erupting Big Island".
And the solution, which is a string, will be "That's so mainland". Okay! Now after that I put an "else" and a single bracket here. Then I'm going to hit this little thing here. And what that'll let me do is I can pop my code in for me like that, where I just drag down and it will take the code that was already there and pop it right in for me.
Alright, so run this. Pick a pepperoni, and it doesn't do anything except there it now puts a little hint sign there. And it says "Try an Erupting Big Island". And you can swipe to the side and say Show Solution and it just tells you that "That's just so mainland". While that was not the original intent of it, for quick output on a live view, assessementStatus can help you when you need a fast alert.
- Comparing iPad and Xcode playgrounds
- Creating and testing Swift classes and code in iPad and Xcode playgrounds
- Prototyping code
- Debugging and testing with Quick Look live views
- Using markup to interactively document code and create educational lessons
- Compiling code into playground books