Learn how to modify the contents of the iPad's code completion bar to include or exclude methods and properties.
- [Instructor] There's one more restriction you might want to make when you are setting up a Playground for educational documentation or presentation purposes. Restricting the code completion bar. I've restricted what you can type in the line editable code by placing in the context that you can only type a number or a string on the Order menu. Close the Pizza Order and go to the Menu Board Playground, drop down to Desserts, if you're not already there. Tap in the end of the editable code block, just after menuBoard prices, press Return.
On the bottom you'll see a fully active code completion bar. In most finished Playgrounds when you are instructing or presenting you'll want to restrict or hide completely that completion bar. Close the iPad Menu Board and go over to Xcode. Open up the MenuBoard, Pizza, go to the Dessert code. We'll start restricting code completion by removing all code completion. Just after the end-hidden-code tag add this code completion tag, code-completion, and then parentheses, everything, hide.
The code-completion tag takes two parameters. The first is the name space where you want to hide or show, the second indicates you are hiding or showing the name space or elements within the name space. If there are elements within the name space you list them after the hide or show. In our first example the name space is everything, so all you need to do is hide it. Close this up and go over to your iPad. Let the file download.
Go to the Desserts page and tap the editable code block, press Return. It is now blank. Close it and go back to Xcode and go back to Desserts. Let's limit the completion to only a few public properties and methods. You can add a few properties and methods back to a completely hidden code completion. So under the line we just did we'll do another one. //#-code-completion, this time make it identifier, so it's going to list identifiers, and again, it's going to be a show this time, and then it's going to be menuBoard, list, prices, print, and printMenu.
Close Xcode again, go to the iPad, wait for it to download, and go into Menu Board. Again, go to Dessert. On the open line tap once and the code completion only shows menuBoard, which is the only context that works here. Tap that and you get another menuBoard. You expect it to show you more than that, but we forgot something. A period is also an identifier, so is an equals, and a plus equals for adding an updating code.
So we're going to have to go back again, shut this down, open menuBoard again, go to Dessert, and I'm just going to copy this line, and change these identifiers to a couple new ones. And so we're going to do show, then it'll be ., space =, space and +=.
Close that up, wait for it to update on your iPad, and go ahead and open it up, go to Dessert, type on menuBoard that you had before, and delete it, and try again. MenuBoard, and you can hit the . now and it actually says list and prices. So we can go ahead and hit list and then =+ and then we get this interesting little odd value function here and for that I'm just going to hit it and then go into the keyboard and change that to Flaming Volcano.
And then I can do menuBoard.prices +=, and then again I have to put my brackets in, and this time I'll just make it 10.95. Run it and you get your Flaming Volcano. Now you don't have to pull up the keyboard, you've got some more kinds of completion things that you can add to this. So let's go and do one more set of adds to this.
So go ahead and close this, open MenuBoard, select Dessert, and I'll copy this again. And we're going to use a different name space this time, so Command + V. I'm going to first change identifier to literal, and then I can go to show will be the same, and then for the items I'm going to be showing there are several types that I can put in here.
One of which is an array. Another one is a string. So now that I've added that I want to close this up again, let MenuBoard update when I go back to my iPad, run it again. And now if I do menuBoard.list = and now I have my array thing all ready for me, and I can do the (mumbling) again.
And, I don't know, I'll make it, instead of a Baked Alaska we'll have a Baked Kauai. And then we do menuBoard.prices and we do values again. Unfortunately there's nothing for a double, there's only integers that we can use for a value here, so we're going to have to actually type this one in. And so I'm going to make that also 10.95 and run my code.
And there you go. I mentioned before that code completion does not prevent the user from pulling up the keyboard and typing code directly. While the for statement does not appear in code completion I could easily write a for loop from the keyboard or the plus. Most people are either lazy or have no idea what commands are available, so code completion takes advantage of that to restrict user input.
- 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