Explore using code found in sources in an iPad playground. Explore how to read code from sources on an iPad. Learn to add sources without Xcode.
- [Instructor] Like the menu board in Xcode I showed you earlier, I'd like to move the class to a source file. How you do that requires a few tricks to get the file into sources, tricks you can do with or without Xcode. On your iPad, close the playground. Open the menu board you created earlier or download a copy from the downloads folder. Hide the live view if it's visible and hit the three dots in the upper right corner. This is the tools menu. At the top is help. In the middle, a set of sharing options.
On the very bottom is the advanced menu. Tap that and you'll see view auxiliary source files. Tap that and you'll get the file structure of the playground. Tap resources and surf pizza and you'll see the pizza image we use there. Hit back and hit sources and then menu board. You'll find the code we wrote earlier. For any playground, you can look at that source code, but you can't change it.
Tap on the code and you can find you can select something and copy. But you can't fix anything, which is a problem because down here you'll notice that I'm supposed to have a zero and not a one, so how do I go about fixing things like this? How do I add sources for my playgrounds? You can, of course, use Xcode as we did before, but I want to point out something here. All these files are text files. You can use any text editor to change or arrange them.
Go ahead and close this playground. Hit done and close. Go back to your desktop. I'm going to show you how to use finder and a text editor to edit sources. Find the menu board playground file. Hit show package contents. Inside the contents, you'll find all of the files we saw before. Click sources. Right click on MenuBoard.swift and then open with and I'll use TextEdit.
That opens the text editor and I'm going to slide down here to print menu, go ahead and change that to zero, hit Command + S to save it, and close it. If I go back to menu board and run it, hit the little arrow to go to dessert and run my code, you'll see the mango moloka'i lime pie is now showing when it didn't before.
Okay, go ahead and close that and now we're going to try to put a whole source file in using a text editor. So go back to your Mac. Go back to playgrounds. Right click on Huli Pizza Order and show package contents. This time, because we don't have a book, we have just the contents.swift file and a few other things including our resources and there's our icons.
Right click in this column to make a new folder. Name the folder sources, copy contents.swift, and paste it in sources. Rename the file Order Table View Controller.swift. Right click and open it with text editor.
In the bottom, delete all the extra code that isn't part of the class so that only the classes here. Make the class public and then the two properties public. Every that's an override also needs to be public, so go ahead and make those all public.
Go ahead and save the file and close it. Now open the contents.swift file. Right click, open with your text editor, and delete the class. Then you can close this and close your finder. Go back to your iPad. Open Huli Pizza Order again.
The playground should now have a small amount of code. If you run it... So we've got some errors. We can see that they're all on line 22, it appears to be, so what we're going to do is we're going to go ahead and fix that. I'm going to hit done and I'm going to do it using Xcode this time. So what I'm going to do is go back to my finder, go to Huli Pizza Order, show package contents, and it's probably my sources is what they're talking about here, so I can now go here and open with Xcode.
And that gives me some line numbers. If you have a text editor with line numbers, you can of course use that, so you can see what I did. On line 22, I put public after func instead of in front of it. Delete that, go here, and change it. Command + S to save it, close this. iCloud looks like it's done its job. I can close this up and I'm going to drop back out of this just to make sure I get it.
Okay and then go ahead and go into Huli Pizza Order again and run my code and there it works. You can of course do all of this in Xcode, but sometimes you don't have Xcode handy or Xcode takes longer to use to make a minor change. Using a text editor means you can edit files on the fly in both PCs and Macs. You do have one disadvantage though. No warnings about errors until it's too late. Once you make a change, you'll only get compiler errors in your playgrounds, so check your code before you use it.
- 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