Learn Swift by Building Applications
上QQ阅读APP看书,第一时间看更新

How to add resource to a playground

Follow these steps to add an image to your playground. Try to use a .png or .jpg—images look great once you load them in a playground:

  1. Open the Navigator panel (cmd + 1)
  2. Select the Resources folder
  3. Press the + button at the bottom of the Navigator panel
  1.  Select Add Files to 'Resources'...
  2. The file is automatically copied to the Resources folder
  3. Try to use it in the main playground

For example, to use an image in the iOS playground you can use the following code:

let img = UIImage(named: "open_xcode.png")

Where the name of the file is open_xcode.png. Don't worry about the code and what UIImage means. We will understand this a bit later on in the book. Take a look at the following screenshot:

Adding an image to your playground