36
iOS applications with Swift Make swiftly iOS development Telerik Academy http://academy.telerik.com Telerik Academy Plus

IOS Apps With Swift

Embed Size (px)

DESCRIPTION

This document for IOS development

Citation preview

Page 1: IOS Apps With Swift

iOS applications with Swift

Make swiftly iOS development

Telerik Academyhttp://academy.telerik.com

Telerik Academy Plus

Page 2: IOS Apps With Swift

About Me Doncho Minkov

Senior Technical Trainer@ Telerik Software Academy

Contestant in the Informatics competitions

Experience with Web and Mobile apps

Proficient with JavaScript and .NET Email: doncho.minkov [at]

telerik.com Blog: http://minkov.it

Page 3: IOS Apps With Swift

Table of Contents Swift overview Language syntax and specifics

Variables, structures and optional values

Control structures: if-else, switch, loops

Functions as first-class objects, closures, etc…

OOP: classes, protocols, enums, extensions

Syntactic sugar: destructuring assignments

iOS applications with Swift Handling gestures on the UI

Using graphics and animations

Page 4: IOS Apps With Swift

Swift OverviewWhat and Why?

Page 5: IOS Apps With Swift

Swift Overview Swift is an innovative new programming language for Cocoa and Cocoa Touch Writing code is interactive and fun

The syntax is concise yet expressive

Apps run lightning-fast

Swift can be used for creating brand-new iOS and OS X apps Or to add features to applications

written with Objective-C

Page 6: IOS Apps With Swift

Swift Overview Swift was introduced at WWDC 2014 as the "new way to create apps for iPhone and iPad" Objective-C without the C

Descendant to Objective-C

No pointers

Functions as first-class objects

Influenced from many modern languages: JavaScript, Objective-C, C#, Python,

Scala, etc…

Page 7: IOS Apps With Swift

Swift Experiment Techniques

Since Swift is pretty new and still needs exploring Apple has provided ways to experiment the new features: REPL (Read Eval Print Loop)

Run swift code from the Terminal

Xcode interactive Playground Immediate code evaluation, while

typing

Page 8: IOS Apps With Swift

Using the Swift REPL

To use the REPL of Swift follow the steps:

1. Start Terminal.app

2. Type

3. Type code directly inside the Terminal

4. The output is shown directly on the Terminal

You can also execute code from a swift file:

$ swift

$ swift print-primes.swift

Page 9: IOS Apps With Swift

Using the Swift REPLLive Demo

Page 10: IOS Apps With Swift

Using the Swift Playground

Apple has provided a Playground for experimenting with Swift features inside Xcode Write code and receive immediate

result

Great for exploring Swift features and research

Allows fast testing of features and syntax

Page 11: IOS Apps With Swift

Going to the Playground

Live Demo

Page 12: IOS Apps With Swift

Swift Syntax and Specifics

Laying the groundwork

Page 13: IOS Apps With Swift

Swift Syntax Overview Swift syntax:

Variables and contants Control structures

If-else, loops, switch

Data structures Arrays, dictionaries, sets

Classes and structures Init functions, methods, properties,

fields OOP

Protocols, extensions, inheritance

Page 14: IOS Apps With Swift

Data types, Variables and Constants

var and let, primitive and reference, functions

Page 15: IOS Apps With Swift

Data Types in Swift Swift supports the common data

types, coming from Objective-C Int for creating integers numbers

UInt for creating unsigned integer numbers

Float, Double for floating-point numbers

Bool for Boolean values

[] for array, [Int] for array of integers Swift supports both structures and

classes Structures create primitive types

Classes create reference types

Page 16: IOS Apps With Swift

Data Types, Variables and Constants

Swift provides two keywords for creating variables: let and var

var creates a mutable variable Its value can be changed any time

The type can be provided or inferred let creates immutable variable

If the type is struct, its value cannot be changed

If the type is object, it can be changed only internally

Page 17: IOS Apps With Swift

Using let and var Creating a variable of type integer:

Using letlet numberOfCourses: Int = 15numberOfCourses = 16 // compiler error

Using varvar count :Int = 15count++ //this is Ok

Variable types are inferred

The compiler will understand that you

want Int

Works with let and varlet numberOfCourses = 15 var count = 15

Page 18: IOS Apps With Swift

Using var and letLive Demo

Page 19: IOS Apps With Swift

Defining Functions in Swift

Functions in Swift have the following syntax: Function returning void:func printMessage(message: String) { println(String(format: "Message: %@", message)) }

Function returning integer:func sum(x: Int, y: Int) -> Int { return x + y}

Function with parameters put inside an array:func sum(numbers: Int…) -> Int {

var sum = 0 for number in numbers { sum += number} return sum}

Page 20: IOS Apps With Swift

Functions in SwiftLive Demo

Page 21: IOS Apps With Swift

Control Flow Structures:

if-else and switch Swift supports the standard control flow structures:

if condition { //run code}else if condition2 { //run other code}else { //run third code}

switch language {case "Objective-C", "Swift": println("iOS")case "Java": println("Android")case "C#": println("Windows Phone")default: println("Another platform")}

switchif-else

Page 22: IOS Apps With Swift

If-else and SwitchLive Demo

Page 23: IOS Apps With Swift

Control Flow Structures: Loops

Swift supports the standard control flow structures: for loop

for i in 0...11 { //i gets the values from 0 to 11, inclusive}

for i in 0..<11 { //i gets the values from 0 to 10, inclusive}

while loopwhile condition { //run code}

Page 24: IOS Apps With Swift

Swift Data Structures

Page 25: IOS Apps With Swift

Swift Data Structures Swift has built-in arrays and

dictionaries let creates immutable

dictionary/array var creates mutable dictionary/array

Immutable arrays:let numbers: [Int] = [1, 2, 3, 4, 5]numbers[2] = -1 //compile errornumbers.append(6) //compile error

Mutable arrays:var names: [String] = ["Doncho", "Ivaylo"]names.append("Nikolay") //Doncho, Ivaylo, Nikolaynames.append("Pesho") //Doncho, Ivaylo, Nikolay, Peshonames[3] = "Evlogi" //Doncho, Ivaylo, Nikolay, Evlogi

Same for dictionaries

Page 26: IOS Apps With Swift

Arrays and Dictionaries

Live Demo

Page 27: IOS Apps With Swift

Classes and Structures

Introducing the OOP principles in Swift

Page 28: IOS Apps With Swift

Swift Classes Swift is an OOP language

Has classes, structures and protocolsclass Square{

var x: Float var y: Float var side: Float init(x: Float, y: Float, side: Float){ self.x = x self.y = y self.side = side } func calcArea() -> Float{ return self.side * self.side } }

Page 29: IOS Apps With Swift

Creating Simple Classes

Live Demo

Page 30: IOS Apps With Swift

Protocols in Swift Protocols in Swift are pretty much

the same as in Objective-C Almost like interfaces in C# and Java They provide a public interface for the

conforming classes to implementprotocol Movable{ func moveToX(x: Int, y: Int) func moveByDx(dx:Int, dy: Int)}

class Shape: Movable{ func moveToX(x: Int, y: Int){ //implementation } func moveByDx(dx: Int, dy: Int){ //implementation } }

Page 31: IOS Apps With Swift

Protocols in SwiftLive Demo

Page 32: IOS Apps With Swift

UIKit Animations

Page 33: IOS Apps With Swift

UIKit Animations A limited number of animatable

properties Custom animatable properties cannot be

created All animations are disabled by default Animations are enabled only when using

special constructslet view = UIView(frame: CGRectMake(x, y, w, h))

UIView.animateWithDuration( 1.0 animations: { let frame = CGRectMake(x + 100, y + 100, w, h) view.frame = frame });

Page 34: IOS Apps With Swift

UIKit Animations: Animation Blocks

UIView.animateWithDuration( 1.0 delay: 1.0 options: UIViewAnimationOptions.CurveEaseIn animations: { let frame = CGRectMake(x + 100, y + 100, w, h) view.frame = frame }, completion: { view.alpha = 0.5});

UIKit Animations with Animation Blocks

Page 35: IOS Apps With Swift

UIKit AnimationsLive Demo

Page 36: IOS Apps With Swift

форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно

програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки

уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop

уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC

безплатен курс "Разработка на софтуер в cloud среда"

BG Coder - онлайн състезателна система - online judge

курсове и уроци по програмиране, книги – безплатно от Наков

безплатен курс "Качествен програмен код"

алго академия – състезателно програмиране, състезания

ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NETкурсове и уроци по програмиране – Телерик академия

курс мобилни приложения с iPhone, Android, WP7, PhoneGap

free C# book, безплатна книга C#, книга Java, книга C#Дончо Минков - сайт за програмиранеНиколай Костов - блог за програмиранеC# курс, програмиране, безплатно

?

? ? ??

?? ?

?

?

?

??

?

?

? ?

Questions?

?

iOS Apps with Swift

http://academy.telerik.com