Introduction to
iOS Programming and Swift
Enoch Huang 黃心和
Outline
• Programming for Apple
• Swift
• Steps to create an iOS app
• An iOS app example
Outline
• Programming for Apple
• Swift
• Steps to create an iOS app
• An iOS app example
Programming for Apple (1/9)
Mac macOS
OS X
iPhone/iPad iOS
Apple Watch watchOS
Apple TV tvOS Apple Devices
Programming for Apple (2/9)
Two Programming Languages
Objective-C
- Developed since early 1980s - Superset of C
- Still popular
Swift
- Unveiled in June 2014
- Ideas from many programming languages - Open source since December 2015
Programming for Apple (3/9)
How to learn?
Start with Swift - Easier to learn
- Latest resources for beginners are in Swift, not Objective-C. - I ll new to everybody
Then Objective-C - More resources
- B l n al a pl . - For iOS developer career
Programming for Apple (4/9)
1. Computer – Mac
2. Account – Apple ID
3. Development Environment – Xcode
Development Requirement
Programming for Apple (5/9)
Mac
Development Requirements – 1. Computer
Programming for Apple (6/9)
If you have an iPhone, you should already have an Apple ID.
Otherwise, create an Apple ID for free.
Development Requirements – 2. Account Apple ID
Programming for Apple (7/9)
Development Requirements – 2. Account
• Individual/Organization - US$99/year for App Store Distribution
• Enterprise Program – US$299/year for In-House App Distribution
• iOS University Program – Free for App Exchange between members
Apple Developer Programs
FYI only
Programming for Apple (8/9)
- Integrated Development Environment (IDE) - Free
- For OS X / iOS / watchOS / tvOS - Simulators included
Development Requirements – 3. Development Environment
Xcode
Build & Run Xcode
Simulator Project
Navigator
Debug Area
Programming for Apple (9/9)
Outline
• Programming for Apple
• Swift
• Steps to create an iOS app
• An iOS app example
The Swift programming language (1/9)
- A fast growing programming language: Development started - 2010
Unveiled - June 2014
Swift 1.0 - September 2014
Swift 2.0 - September 2015 (try/catch) Swift 3.0 - Fall 2016? (work with C++) - Became Open Source in December 2015
(Available on Linux now)
History 雨燕
The Swift programming language (2/9)
- More efficient than Objective-C & Python
The Apple Worldwide Developers Converence (WWDC) 2014
The Swift programming language (3/9)
Swift Resources
Official Guide - The Swift Programming Language Tutorials - Ray Wenderlich
AppCoda (AppCoda中文版)
Q&A - Stack Overflow Slides - SlideShare
Video- Getting Started with Swift Ebook- Swift 起步走
Don’t buy any book!! Swift books become
outdated quickly.
The Swift programming language (4/9)
Learn Swift without a Mac!
Free website:
IBM Swift Sandbox
The Swift programming language (5/9)
Hello, World!
Constant
String
Show
No Semicolon ( ; ) let str = "Hello, World!"
print(str)
IBM Swift Sandbox Example
The Swift programming language (6/9)
Type Inference
IBM Swift Sandbox Example
let lab : String = "WIDM” let floor : Int = 3
Type Declaration
let lab = "WIDM” let floor = 3
The Swift programming language (7/9)
Switch Case
IBM Swift Sandbox Example
let condition = true if condition {
print("Condition is true!!") }
If Condition
var myCondition = 5
switch myCondition { case 0:
print("This is zero.") case 1:
print("myCondition is one.") case 2,3:
print("I like number two or three.") case 4...10:
print("Four to ten.\nfallthrough") fallthrough
default:
print("The default case.") }
The Swift programming language (8/9)
IBM Swift Sandbox Example
For Loop
for count in 0...5 {
print("count=\(count)") }
Condition //Explicit format:
var fruitList: Array<String> = ["apple", "banana"]
//Explicit shortcut format:
var fruitList: [String] = ["apple", "banana"]
//Type inference
var fruitList = ["apple", "banana"]
Array
The Swift programming language (9/9)
More …
While Loops, Array Manipulation, Dictionaries, Functions, Closures, Enumerations, Classes, Structures, Inheritance, Optional, Error Handling, Type Casting, Protocols…
Outline
• Programming for Apple
• Swift
• Steps to create an iOS app
• An iOS app example
Steps
Steps to create an iOS app (1/15)
1. Add Apple ID account to Xcode. 2. Create a new Xcode project.
3. Run the app. 4. Start coding.
Steps
Steps to create an iOS app (2/15)
1. Add Apple ID account to Xcode.
2. Create a new Xcode project. 3. Run the app.
4. Start coding.
1. Add Apple ID Account to Xcode
Steps to create an iOS app (3/15)
(1) Run Xcode.
(2) Select Xcode Preferences
(3) Select Accounts + sign Add Apple ID
Steps to create an iOS app (4/15)
(4) Enter the Apple ID (email address) and password. Select Sign In.
(5) The account information such as team and role of the Apple ID is shown as below.
1. Add Apple ID Account to Xcode
Steps
Steps to create an iOS app (5/15)
1. Add Apple ID account to Xcode. 2. Create a new Xcode project.
3. Run the app. 4. Start coding.
Steps to create an iOS app (6/15)
(1) Create a new project
2. Create a new Xcode project
(2)
Select iOS Application
Single View Application
Next
Steps to create an iOS app (7/15) 2. Create a new Xcode project
(3) Configure project settings:
• Product name
Organization Name/Identifier
• Language (Swift/Objective-C)
• Devices (iPhone/iPad/Universal)
• CoreData and Unit/UI tests options
Steps
Steps to create an iOS app (8/15)
1. Add Apple ID account to Xcode. 2. Create a new Xcode project.
3. Run the app. 4. Start coding.
Steps to create an iOS app (9/15)
1. Select the iOS simulator or device 2. Click
the Run button.
3. Run the app
Steps to create an iOS app (10/15)
Wait for the simulator to launch.
Ignore any error messages.
Run Run
A white screen should be
displayed.
3. Run the app
Steps
Steps to create an iOS app (11/15)
1. Add Apple ID account to Xcode. 2. Create a new Xcode project.
3. Run the app. 4. Start coding.
. - 4s (Before iOS 10) - 5 / 5s / 5c / SE
.7 - 6 / 6s
. - 6 Plus / 6s Plus
Source:http://www.idev101.com/code/User_Interface/sizes.html
iPhones on market
3:2 16:9
2011 ’12/’13/’16 2014/2015
Steps to create an iOS app (12/15) 4. Start coding
Storyboard
Programmatically
Two UI implementation methods
Steps to create an iOS app (13/15) 4. Start coding
ViewController Structure
4. Start coding
Steps to create an iOS app (14/15)
Steps to create an iOS app (15/15)
Open ViewController.swift and add the two lines in func viewDidLoad(): view.backgroundColor = UIColor.orangeColor()
print("Hello, World!")
Called when app is started.
UIKit Framework
4. Start coding
Outline
• Programming for Apple
• Swift
• Steps to create an iOS app
• An iOS app example
An iOS app example (1/7)
• Button at the center
• Change the background color
A Simple Example
Storyboard
Two UI implementation methods
An iOS app example (2/7) ViewController.swift
Programmatically
An iOS app example (3/7)
• Button added to the root view.
• Background color of
the root view is changed.
ViewController Structure
No child views in this example.
ViewController.swift
An iOS app example (4/7)
(1) Add a UIButton to the root view of the ViewController in func viewDidLoad():
Size and position
Show in view
Color
ViewController.swift
An iOS app example (5/7)
(2) Add an action pressed() to the UIButton:
ViewController.swift
An iOS app example (6/7)
(3) Create a constant array of various colors and a color variable above func viewDidLoad():
(4) Modify the button action function pressed() as below:
ViewController.swift
An iOS app example (7/7)
Complete code:
Outline
• Programming for Apple
• Swift
• Steps to create an iOS app
• An iOS app example
W a Nex ?