IOS Apps With Swift

Preview:

DESCRIPTION

This document for IOS development

Citation preview

iOS applications with Swift

Make swiftly iOS development

Telerik Academyhttp://academy.telerik.com

Telerik Academy Plus

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

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

Swift OverviewWhat and Why?

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

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…

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

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

Using the Swift REPLLive Demo

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

Going to the Playground

Live Demo

Swift Syntax and Specifics

Laying the groundwork

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

Data types, Variables and Constants

var and let, primitive and reference, functions

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

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

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

Using var and letLive Demo

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}

Functions in SwiftLive Demo

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

If-else and SwitchLive Demo

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}

Swift Data Structures

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

Arrays and Dictionaries

Live Demo

Classes and Structures

Introducing the OOP principles in 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 } }

Creating Simple Classes

Live Demo

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 } }

Protocols in SwiftLive Demo

UIKit Animations

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 });

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

UIKit AnimationsLive Demo

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

програмиране за деца – безплатни курсове и уроцибезплатен 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

Recommended