IOS Apps With Swift

Published on June 2016 | Categories: Documents | Downloads: 53 | Comments: 0 | Views: 290
of 36
Download PDF   Embed   Report

This document for IOS development

Comments

Content

iOS applications
with Swift
Make swiftly iOS development

Telerik Academy Plus
Telerik Academy
http://academy.telerik.com

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

Swift Overview
What 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
swift
2. Type
3. Type code directly inside the
Terminal
4. The output is shown directly on
the Terminal
$ swift print-

.You

can primes.swift
also execute code from a
swift file:

Using the Swift REPL
Live 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



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 let
let numberOfCourses: Int = 15
numberOfCourses = 16 // compiler error

 Using var
var count :Int = 15
count++ //this is Ok

 Variable types are inferred
 The compiler will understand that you
want Int
 Works with let and var
let numberOfCourses = 15

var count = 15

Using var and let
Live 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
ansum(numbers:
array: Int…) -> Int {
func
var sum = 0
for number in numbers { sum += number}
return sum
}

Functions in Swift
Live Demo



Control Flow
Structures:
if-else and switch

Swift supports the standard
control flow structures:
if-else
if condition {
//run code
}
else if condition2
{
//run other code
}
else {
//run third code
}

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

If-else and Switch
Live 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 loop
while 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

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



Mutable arrays:

var names: [String] = ["Doncho", "Ivaylo"]
names.append("Nikolay") //Doncho, Ivaylo, Nikolay
names.append("Pesho")
//Doncho, Ivaylo, Nikolay, Pesho
names[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
protocols
class 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 implement
protocol 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 Swift
Live 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
let view = UIView(frame: CGRectMake(x, y, w, h))
special
constructs
UIView.animateWithDuration( 1.0
animations: {
let frame = CGRectMake(x + 100,
y + 100,
w,
h)
view.frame = frame
});

UIKit Animations:
Animation Blocks


UIKit Animations with 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
Live Demo

iOS Apps with Swift
?

?

?

?

?

?

?

?

?

?
?

?

?

?

BG Coder - онл айн съст езат елна сист ема - online judge
форум програми ране, форум уеб ди зайн
ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET
ASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC
алго академи я – съст езат елно програмиране, съст езания
курс мобилни приложения с iPhone, Android, W P7, PhoneGap
Дончо Минков - сай т за програмиране
Николай Кост ов - блог за програми ране
C# курс, програми ране, безплат но

?

?
курсове и уроци по програми ране, уеб дизайн – безплат но
курсове и уроци по програми ране – Телери к академия
уроци по програми ране и уеб ди зай н за учени ци
програмиране за деца – безплат ни курсове и уроци
безпл ат ен SEO курс - опт и ми зация за т ърсачки
курсове и уроци по програми ране, книги – безпл ат но от Наков
уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop
free C# book, безплат на книга C#, книга Java, книга C#
безплат ен курс "Качест вен програмен код"
безплат ен курс "Разработ ка на софт уер в cloud среда"

?

?

Questions?

?

http://academy.teler

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close