Learning To Use Xcode

Toolbar area

  • scheme pop-up menu; selects which device

  • run button (looks like play button)

Navigator area
Editor area
Utility area

Simulator: An app in Xcode that simulates the behavior and appearance of running an app on a device.

  • The simulator gives you an idea of how your app would look and behave if it were running on a device.

3 ways to run:

  • Run button in toolbar

  • Product > Run

  • press Command-R

2 ways to quit the simulator:

  • Simulator > Quit Simulator

  • press Command-Q

To look at the source file:

  1. Make sure the project navigator is open:

View > Navigators > Show Project Navigator

Or press the icon to the utmost left in the navigator selector bar.

project navigator: displays all the files in your project

  1. Open project file

  2. press AppDelegate.swift

    • double-clicking opens it in a different window

The App Delegate Source File

Two primary functions:

  • it defines your AppDelegate class

  • it creates the entry point to your app and a run loop that delivers input events to your app

Swift Language

var myFirstProgram: String
myFirstProgram = “Hello World

  1. create variable:
    var age

  2. declare variable:
    var age: Int

  3. Initialize (give it a value):
    age = 26

Example Code:

var age: Int
age = 26
print(“You are \(age) years old.”)

To skip specifying the type:

var name = “Ana”
print(“Great work\(name)!”)

**When you initialize a variable with a value, Swift will infer the type.

Assign a new value to the variable:

var feeling = “awesome”
// How are you feeling?
feeling = “tired”
print(“Your mood is \(feeling)!”)

Once a constant is set, its value cannot be changed.

// Contants start with let instead of var
let secondsPerMinute = 60

** Since 1 min = 60 seconds, using let (a constant) is safer because you will see and error message if you accidentally try to change its value.

Combining strings:

let appName = “Code Swift”
var review = “ is a fantastic app!”
var message = appName + review

The process of creating longer strings by including variables or constants within a string:

var language = “Swift”
print(“I am learning \(language)”)

var company  = “Apple”
let yearFounded = 1976
var str = “ was founded in “
print(company + str + String(yearFounded))

To create a constant, declare and set it’s value:

let fahrenheit: Float = 50

**Since an integer was set as the value, it is necessary to specify the type as float

let celsius: Float
let Fahranheit: Float = 50
celsius = (Fahrenheit - 32) / 1.8