IOS Apps With Swift

  • View
    4

  • Download
    1

Embed Size (px)

DESCRIPTION

This document for IOS development

Transcript

PowerPoint Presentation

iOS applications with SwiftMake swiftly iOS developmentTelerik Academyhttp://academy.telerik.com Telerik Academy PlusAbout MeDoncho MinkovSenior Technical Trainer@ Telerik Software AcademyContestant in the Informatics competitionsExperience with Web and Mobile appsProficient with JavaScript and .NETEmail: doncho.minkov [at] telerik.comBlog: http://minkov.it

Table of ContentsSwift overviewLanguage syntax and specificsVariables, structures and optional valuesControl structures: if-else, switch, loopsFunctions as first-class objects, closures, etcOOP: classes, protocols, enums, extensionsSyntactic sugar: destructuring assignmentsiOS applications with SwiftHandling gestures on the UIUsing graphics and animationsSwift OverviewWhat and Why?Swift OverviewSwift is an innovative new programming language for Cocoa and Cocoa TouchWriting code is interactive and funThe syntax is concise yet expressiveApps run lightning-fastSwift can be used for creating brand-new iOS and OS X appsOr to add features to applications written with Objective-CSwift OverviewSwift was introduced at WWDC 2014 as the "new way to create apps for iPhone and iPad"Objective-C without the CDescendant to Objective-CNo pointersFunctions as first-class objectsInfluenced from many modern languages:JavaScript, Objective-C, C#, Python, Scala, etc

Swift Experiment TechniquesSince 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 TerminalXcode interactive PlaygroundImmediate code evaluation, while typingUsing the Swift REPLTo use the REPL of Swift follow the steps:Start Terminal.appType Type code directly inside the TerminalThe output is shown directly on the TerminalYou can also execute code from a swift file:$ swift$ swift print-primes.swiftUsing the Swift REPLLive DemoUsing the Swift PlaygroundApple has provided a Playground for experimenting with Swift features inside XcodeWrite code and receive immediate resultGreat for exploring Swift features and researchAllows fast testing of features and syntaxGoing to the PlaygroundLive DemoSwift Syntax and SpecificsLaying the groundworkSwift Syntax OverviewSwift syntax:Variables and contantsControl structuresIf-else, loops, switchData structuresArrays, dictionaries, setsClasses and structuresInit functions, methods, properties, fieldsOOPProtocols, extensions, inheritanceData types, Variables and Constantsvar and let, primitive and reference, functionsData Types in SwiftSwift supports the common data types, coming from Objective-CInt for creating integers numbersUInt for creating unsigned integer numbersFloat, Double for floating-point numbersBool for Boolean values[] for array, [Int] for array of integersSwift supports both structures and classesStructures create primitive typesClasses create reference typesData Types, Variables and ConstantsSwift provides two keywords for creating variables:let and varvar creates a mutable variableIts value can be changed any timeThe type can be provided or inferredlet creates immutable variableIf the type is struct, its value cannot be changedIf the type is object, it can be changed only internallyUsing let and varCreating a variable of type integer:Using letlet numberOfCourses: Int = 15numberOfCourses = 16 // compiler errorUsing varvar count :Int = 15count++ //this is OkVariable types are inferredThe compiler will understand that you want IntWorks with let and varlet numberOfCourses = 15var count = 15Using var and letLive DemoDefining Functions in SwiftFunctions 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 DemoControl Flow Structures:if-else and switchSwift 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-else21If-else and SwitchLive DemoControl Flow Structures: LoopsSwift supports the standard control flow structures:for loopfor i in 0...11 { //i gets the values from 0 to 11, inclusive} for i in 0.. Float{ return self.side * self.side } }Creating Simple ClassesLive DemoProtocols in SwiftProtocols in Swift are pretty much the same as in Objective-CAlmost like interfaces in C# and JavaThey 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 DemoUIKit AnimationsUIKit AnimationsA limited number of animatable propertiesCustom animatable properties cannot be createdAll animations are disabled by defaultAnimations 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 BlocksUIView.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 BlocksUIKit AnimationsLive DemoiOS Apps with Swifthttp://academy.telerik.com , , 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, PhoneGapfree C# book, C#, Java, C# - - C# , , ??????????????????Questions??