Git Product home page Git Product logo

swift-playground-readme-dc-web-062419's Introduction

Playgrounds

Coco

How many cares one loses when one decides not to be something but to be someone. ~Coco Chanel

Overview

In this lesson, we'll introduce playground files in Xcode and how to access and write code with them.

Learning Objectives

  • Create a new playground file using Xcode.
  • Explain that a playground file is used solely as a place to explore code without the need to create a project
  • Write code in a playground file
  • Recognize error message that appear when trying to change a let constant.

Playgrounds

Apple provides an excellent tool for experimenting with Swift: Playgrounds. Playgrounds offer you a scratchpad for testing out Swift code. You can not only see the results of code as you type them in, but you can gain insight on what exactly is being executed by various bits of Swift code. Best of all, the tool is free and is a part of Apple's Xcode programming suite.

Playgrounds ships as part of Xcode, so the first thing you must do is download Xcode. You will need a Mac to run Xcode, as it does not run on iOS or Windows PCs. You can download Xcode from the App Store on OS X.

Once Xcode has been downloaded, open it up from the Applications folder or launcher. You will be greeted by the following splash screen. Click "Get started with a playground" to create a new playground and experiment with some Swift code.

Get started with a playground

After clicking on "Get started with a playground", you will be prompted to name your new playground file. Give it a memorable name.

You can also select the programming environment you want to experiment with: iOS, OS X, or tvOS. Different environments offer slightly different options for interacting with the underlying system; for example, iOS provides the Cocoa Touch framework, whereas OS X provides the Cocoa framework. For now, the code you are writing will not depend on any specific platform, so you can select "iOS".

Setup your playground

Click "Next" to continue. You will be prompted to save your new playground file somewhere on your computer. Save it anywhere you like and click "Create" to finish setting up your new playground. You will see a mostly empty document like the one below.

Your new playground

Experimenting With Code

By default, your new playground will include a line importing UIKit (import UIKit) and the declaration of a string variable called str. You can erase the contents of the playground; for now, you won't be experimenting with any UIKit features.

You can write any Swift code you want in the playground file. As you enter each line, you will see the results of executing that line of code in the grey sidebar on the right side of the playground. Try declaring a constant called str and setting it to "Hello, world!". You should see the following in the playground file:

Playground output

Since you declared str to be a constant, try changing its value on the second line of the playground file. Notice that instead of executing that line of code and showing its output in the sidebar, Xcode instead highlights it as an error:

Playground error highlighting

If you click on the red circle to the left of the erroneous line of code, Xcode will tell you why the line was marked as an error, and even provides a suggestion on how to fix it:

Playground error explanation

Do you know why Xcode marked it as an error?

Don't worry about fixing the error right now. Just be aware that this is how Xcode will show you lines of Swift code that won't work, and show you one way to fix them so they do work. It might not always be the most correct way to fix code, though!

That's all you need to know about playgrounds for now, but you'll be making extensive use of them as you progress through your Swift studies. For more information, feel free to read Apple's documentation on playgrounds.

View this lesson on Learn.co

swift-playground-readme-dc-web-062419's People

Contributors

annjohn avatar jimcampagno avatar mdippery avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.