iOS Development Help Site
By Nancy Liu
Dept. of Computer Science
iOS Help Site Navigation
iOS Help Site Home
Swift: Basics and Intros
Swift vs. Other Languages
Language Basics
Setup: Getting Started in Xcode
Playgrounds
Projects
Playground vs. Project
Stuck?
Projects: Basics in Xcode
The Interface Builder
Controllers
Views
Additional Features
Projects: Not as Basic
Segues
Delegating Protocols
Core Data and User Defaults
Multithreading
KVO
Extras
Introduced in Class
Others

Other useful links
Key Dates 2016-17
Final Exam Schedule
Register your i>clicker
Unicode
ASCII (Wikipedia)

Getting Started

Playgrounds

Playgrounds are quick to set up, and quick to use. These can be used as a testing grounds for when you are unsure how something in a specific scope works, or just for practice.

Xcode provides an easy way to set up a playground for your convenience. When you open up a new file, you will have the option to create a new playground.

Additionally, if you're just looking for practice or don't feel like having multiple windows or instances of Xcode open, there are online alternatives, such as  iswift.org. Be aware though, that learning the language itself may be different from learning Xcode's exact way of coding. Practicing like this should be just for practicing the Swift language, not to learn how to code in Xcode specifically.

Below is a quick run-through of setting up a Playground.

 

 

When you open up Xcode, this is typically the window you find first (unless you specifically unchecked the box at the bottom). On the right side will be a list of files that you've recently opened or worked on that you can quick access, if you have any.

 

After you choose to create a playground, this is the window you should get next. Hitting the Previous button will bring you to the File Template selection - so if you're not particularly sure what file type to use just yet, stick with the default they give you for now.

Now, you can name your playground whatever you want, and then choose a save location for it.

 

You done it. Your Xcode window may or may not look like this. If you would like the extra help windows (they are especially helpful for Projects), the top-right has three options: the left side for the  Navigator, the bottom for the Debug area, and the right side for  Utilities.

The Navigator is for navigating across multiple files within a single folder or project.

The Debug area is fairly self explanatory. Here, you will find the  Execute Playground button, which is the blue play button. Normally, the Playground will automatically execute in whole when you make any changes, and throw errors in the Debug area when it cannot.

Utilities gives you information about the current file itself, and allows you to edit or add extra features (especially when you get to editing the Storyboard). Most importantly in the  Utilities section (for the playground) will be the File Inspector, which is the page icon with one of the corners folded - next to the question mark (?) icon. This section will be more populated in projects, when you work with a Storyboard.

And this is essentially the basics about the Playground! If you just want to plug-and-play a testing area, you are now set to do whatever you like. If you're still not satisfied, there are various file templates for different types of media.

 

So, to summarize:

• Open Xcode, and select "Get started with a playground"

• Choose a name for your playground, and save it in the location you want

• The top-right corner that has many buttons is important, as it shows: the Navigator, Debug area, and Utilities window.

• Navigator: how you can move between files (and the .storyboard file, for projects) you want to edit

• Debug: for debugging. Will throw errors and redirect you to the error thread, and give you the specific error name. Very useful for figuring out what exactly you missed, or allows you to look up what error you are experiencing if all else fails.

• Utilities: shows the file information. On the .storyboard file, also has extra windows to choose from that give specific information on certain aspects of the storyboard.

• The playground will automatically and dynamically run when you update/change your code. If you add an extra line of code, the playground will automatically compile and execute completely.

Have fun.

 

Projects

Projects are as you expect: you can have as many individual, inter-connected classes as you need. Anything outside of a project doesn't belong in the project scope (ex: if you're referencing a self-made object class, it has to be in the project to be recognized). If you can't see a file in the  Navigator, make sure it's there, or your app can't access it.

A quick run-through of the set-up for homeworks!

 

 

Projects!! Instead of creating a new Playground, let's create a new Project instead. When you choose the option, you should be met with the screen above. As you notice, there's multiple platforms you can create Projects for - but for the class, let's stick with Single View Application.

On the next window, you'll have prompts to fill out the information for the project itself. For the class, stick to inputting information as instructed on your first homework assignment. This info won't change from homework to homework, so you can reuse it without worry - but it might change for your group project depending on if you want it to be class-only or potentially published.

 

 

Once you name everything and find a save location, you should be immediately greeted with the project screen. For getting started, don't worry about any of the extra files for now, being:  AppDelegate.swiftAssets.xcassetsLaunchScreen.storyboardInfo.plist. You will probably be fiddling around with these later, but not now.

 

To summarize setting up a project:

• Open Xcode, select "Create a new Xcode project"

• Choose your file template - for homeworks, this is typically Single View Application

• Fill out the information you want to label your project with (normally specified in the first homework assignment, which you can keep using)

Have fun.

 

That is the end of setting up a project! Below are project interface extras.

 

The Project Interface vs. The Playground

There are a few extra windows and functions to take note of in the project interface that is different from the playground.

First, the Build & Run button is what you use instead of Execute Playground, and is located at the top right instead of in the Debug  area.

You may notice in the top-right that the buttons are the same, but the Assistant Editor button might be useful to you (the venn-diagram button). This button allows you to have two file windows open - for example, you can have the storyboard on one side, and your .swift file on the other (since you can control-drag aspects of the storyboard into the .swift file).

 ESCAPE='HTML'

 

ADDITIONAL INSPECTORS (OTHER THAN FILE/QUICK HELP)

In the Main.storyboard scene, you will also see that there are 4 extra buttons in the  Utilities section, from left to right: IdentityAttributesSize, and  Connections inspectors.

These four windows will be invaluable when you are editing the storyboard.

The following screenshots use the default gimme View Controller in Main.storyboard.

• Identity: allows you to view/change the class that the current Controller is connected to.

Ex: if you have a Table View Controller in the storyboard that requires specific functions and interactions defined, you can create a .swift file (name, say, TableViewController.swift) that will connect to it and defines the Table View Controller.

By default, the default gimme View Controller in main.storyboard is connected to the gimme ViewController.swift, to give you a base template.

• Attributes: allows you to change specific visual properties and metrics about a specific object in the storyboard. Need a View inside the View Controller to interact or look a certain way? This window is where you change those.

Ex: If you have a label, this is where you would change properties such as: 

• Does the text truncate or wrap to the next line?

• How does the text inside the label align - towards the center, the right, left?

• Automatic sizing?

• Do you want shadows on your letters.

• Size: allows you to change the size of an object, as expected. For Controllers themselves, it is not recommended to change anything, but this is where you change sizes and alignments for objects inside Controllers (such as a label or button). 

In this example, we are looking at the Size parameters of the View that is inside the default View Controller, since we don't want to fiddle with the size of the actual View Controller itself.

• Connections: this lets you see and add/delete connections for an object's interactions. By hovering over the right-hand-side circles, a (+) icon appears (similarly to the left-hand- side in .swift code, when you create a storyboard object). This allows you to connect things directly as needed, or delete things that are messing up. 

Important: when you create a storyboard object in a .swift file through control-drag, that object's connections do not disappear when you delete the object!!! If things are breaking, make sure if you created/deleted objects, that they don't leave residual connections behind.

 

For example: If I create a Button object in the View Controller of Main.storyboard, control-drag to ViewController.swift, and create an IBAction named button, this IBAction is now connected to the Button object in the storyboard. 

HOWEVER, if I delete this line of code, and create another IBAction the exact same way and named it  butt, what happens?

 ESCAPE='HTML'

The connection from the Button object to button still exists, but there's only a butt in the .swift file! Xcode freaks out and things break and can't run. To fix this, we go into Connections and delete the Button's connection to the non-existant button.

 

ADDING/CREATING/DELETING FILES

It's important to note that you can essentially move files into or out of the project, using the  Navigator. You can either create a new file, or add one to the project if you have an existing file. Deleting files directly from the Navigator does not cause major repercussions, but Xcode will throw you errors because it can't find that specific file anymore - you can rename everything as needed.

If you want to create or add new files, the option to do so will be under File, as expected. If you want to create a new file, Xcode will prompt you to choose the file template you want to use. Generally:

Cocoa Touch Class: for when you want to create an Object class (ex: Person, Student, Car, Candidate, etc.). The file template is automatically configured to be a subclass of NSObject for this purpose, but you can change it as you need to.

Swift File: when you need a .swift file, normally to accompany a View Controller that requires  defined functions and interactions.

You can use other files, but these two will be the most used.

 

 

A special note about HOMEWORK ASSIGNMENT 1:

Currently, the first homework assignment is meant to get you situated into a project and getting used to programming with Swift in Xcode. It (as of now) does not require use of a .storyboard file, or ViewController.swift file; you can remove every single file in this new project, and create the ones you need. The output you are looking for should be in the  Debug area.

From Homework 2 and onwards, however, you will 100% be using View Controller .swift files and the main.storyboard file. For this, use as many .swift files and add to main.storyboard as needed!

 

Are you stuck on projects?

If you're stuck in the playground or your project (mainly project, because playgrounds are fairly plug and play), here are some things to double-check:

Things have disappeared from the Storyboard.

Is it set to "Any x Any", or "Compact x Any", or something else altogether? If anything on your Storyboard always looks skewed or completely disappears, check to make sure you have the right dimensions set (or change them to something else and back to the setting you had originally, to see if that refreshes anything).

If things still look skewed (but aren't disappearing), then it may be your constraints and dimensions that aren't sticking. Double-check to make sure everything looks fine in your intended iPhone version; if you intend a fit-all, more work on constraints may be needed.

Check to see what version of the iPhone, iOS, and Xcode you're using!

Different iPhones have different sizes and dimensions, and different iOS versions may run things differently from one another. The same applies to Xcode: someone using Xcode 7 won't be able to open the Storyboard for an Xcode 8 version, even if it's the same file.

Make sure that you always stick with the same versions for everything, from Homework 1 all the way until you finish your group project, even if updates happen and programs default to different things. You will almost 100% never be made to change versions during the semester.

The "Build & Run" button is greyed out.

You may have something in your project that's now wonky. Have you recently changed anything about the project itself, like the name of the project, or project files?

          • Check the Scheme of your project: Product > Scheme > Edit Scheme > Run > Info. If there is no app name selected, and it says "None", try setting it to "Executable".