SlideShare uma empresa Scribd logo
1 de 25
IOS DEVELOPMENT USING SWIFT 
Enums, ARC, Delegation, Closures 
Ahmed Ali
TODAY TOPICS 
• Enumerations 
• Automatic Reference Count (ARC) 
• Delegation pattern 
• Closures 
• Demo: 
• Table view 
• Move to another screen programmatically 
• Pass data between screens 
Ahmed Ali
ENUMERATIONS 
• An enumeration is a value type used to define a common type for a group of related values. 
• Enumeration can have methods and computed properties. 
• Also it can deal with numeric data types, strings, and characters. 
• Example: 
enum Direction 
{ 
case UP 
case DOWN 
case LEFT 
case RIGHT 
} 
Ahmed Ali 
enum Direction : String 
{ 
case UP = "Up" 
case DOWN = "Down" 
case LEFT = "Left" 
case RIGHT = "Right" 
}
ENUMERATIONS (CONT) 
• There are two ways to create enum instances: 
• Using enum case directly. 
• Create instance from a raw value. This is only available if the enum type is specified. 
• Example: 
//Use direct case 
let up = Direction.UP 
//Creating from raw value. 
//fromRaw static method returns an optional type 
let down = Direction.fromRaw("Down") 
Ahmed Ali
ENUMERATIONS (CONT) 
• Usage example: 
//The raw value may come from a user input or a web service response 
let rawDir = "Left" 
if let dir = Direction.fromRaw(rawDir){ 
//The rawDir value is one of our supported directions. Lets see which one it is 
switch dir{ 
case .UP: 
println("Selected direction is Up") 
case .DOWN: 
println("Selected direction is Down") 
default: 
println("Selected direction is (dir.toRaw())") 
} 
} 
Ahmed Ali
ENUMERATIONS (CONT) 
• Enumeration in Swift are more complicated than this, they can have associated values 
with each case. 
• Example: 
enum Shape{ 
case SQUARE(width: Int, height: Int) 
case CIRCLE(centerX: Int, centerY: Int, radius: Int) 
} 
let shape = Shape.SQUARE(width: 50, height: 50) 
Ahmed Ali
ENUMERATIONS (CONT) 
• Checking the value for associated value cases example: 
switch shape{ 
case .SQUARE(width: let width, height: let height): 
println("Square shape with dimension ((width), (height))") 
case .CIRCLE(20, let y, let r) where r > 5: 
println("Circle shape with center point ((20), (y))") 
case .CIRCLE(let x, let y, _): 
println("Circle shape with center point ((x), (y))") 
} 
Ahmed Ali
ARC 
• A garbage collector is not a good solution for a limited resources devices. 
• Automatic Reference Count (ARC) is the memory management mechanism used widely in 
iOS (by Objective-C and Swift). 
• ARC keeps track of how many references refer to each class instance. 
• When these references become zero, the object’s memory space is deallocated. 
• ARC is only applied on class instances; structs and enums are value types, they are not 
stored or passed by reference. 
Ahmed Ali
ARC (CONT) 
• Explanation example: 
class Order{ 
var orderDate : NSDate 
var product : Product 
init(orderDate: NSDate, product: Product){ 
self.orderDate = orderDate;self.product = product 
} 
deinit{ println("Order has been removed from the memory") } 
} 
class Product{ 
var name : String 
init(name: String){ self.name = name } 
deinit{ println("Product (name) has been removed from the memory") } 
} 
Ahmed Ali
ARC (CONT) 
• Explanation example (Cont): 
var product : Product! = Product(name:"iPhone") 
var order : Order! = Order(orderDate: NSDate(), product: product) 
product = nil 
order = nil 
Ahmed Ali
ARC – RETAIN CYCLE 
• All references are strong by default. 
• When two instances strongly refer to each other, they will remain in memory even when 
they are no more in use. 
• Your app may run out of memory and crash. 
Ahmed Ali
ARC – RETAIN CYCLE (CONT) 
• Explanation example: 
class Apartment{ 
var tenant : Person! 
deinit{ println("Apartment has been removed from memory")} 
} 
class Person{ 
var apartment : Apartment! 
deinit{ println("Persone has been removed from memory")} 
} 
var apartment : Apartment! = Apartment() 
var tenant : Person! = Person() 
apartment.tenant = tenant 
tenant.apartment = apartment 
apartment = nil; tenant = nil; 
Ahmed Ali
ARC – RETAIN CYCLE (CONT) 
• Break retain cycle example: 
class Apartment{ 
weak var tenant : Person! 
deinit{ println("Apartment has been removed from memory")} 
} 
class Person{ 
var apartment : Apartment! 
deinit{ println("Persone has been removed from memory")} 
} 
var apartment : Apartment! = Apartment() 
var tenant : Person! = Person() 
apartment.tenant = tenant 
tenant.apartment = apartment 
apartment = nil; tenant = nil; 
Ahmed Ali
DELEGATION PATTERN 
• Delegation pattern allows one type to delegate some of its behaviors to an instance of 
another type. 
• A real-world example will be used in the demo. 
• Example: 
class PhotosSlideshow : UIView 
{ 
func show(){} 
//the rest of the implmenetation 
} 
Ahmed Ali
DELEGATION PATTERN (CONT) 
1. Define a protocol which wraps the needed functionality. 
protocol ImagesDataSource 
{ 
func numberOfImagesInSlideshow(slideShowView: PhotosSlideshow) -> UInt 
func slideShowView(slideShowView: PhotosSlideshow, imageAtIndex: UInt) -> 
UIImage 
} 
Ahmed Ali
DELEGATION PATTERN (CONT) 
1. Use this protocol as weak reference in your delegating type 
class PhotosSlideshow : UIView 
{ 
weak var delegate : ImagesDataSource! 
func show(){} 
//the rest of the implmenetation 
} 
Ahmed Ali
DELEGATION PATTERN (CONT) 
1. Objective-c protocols allows optional requirements. 
@objc protocol ImagesDataSource 
{ 
func numberOfImagesInSlideshow(slideShowView: PhotosSlideshow) -> UInt 
func slideShowView(slideShowView: PhotosSlideshow, imageAtIndex: UInt) -> 
UIImage 
optional func slideShowView(slideShowView: PhotosSlideshow, 
userSelectedImageAtIndex:UInt) 
} 
Ahmed Ali
DELEGATION PATTERN (CONT) 
1. Objective-c protocols allows optional requirements. 
class PhotosSlideshow : UIView 
{ 
weak var delegate : ImagesDataSource! 
func show(){} 
func userDidSelectImage(image: UIImage) 
{ 
delegate?.slideShowView?(self, userSelectedImage: image) 
} 
//the rest of the implmenetation 
} 
Ahmed Ali
CLOSURES 
• A closure encapsulates a code block to be used later. 
• Think of it as an function without a name. 
• A closure can be used as any type, for example it can be used: 
• As the type of a property. 
• As a method’s parameter type. 
• As a return type. 
• Closures are passed by reference. 
Ahmed Ali
CLOSURES (CONT) 
• Example of using Closure as a property type: 
var myClosure : ((param1: Int, param2: String) -> String)! 
• Or define the closure type with tyepalias: 
typealias MyClosureType = (param1: Int, param2: String) -> String 
var myClosure : MyClosureType! 
Ahmed Ali
CLOSURES (CONT) 
• Assigning it a value and calling it: 
myClosure = { 
(firstParam, secondParam) -> String in 
println("First param: (firstParam), Second param: (secondParam)") 
return "(firstParam) and (secondParam)” 
} 
myClosure(param1: 4, param2: "String") 
Ahmed Ali
CLOSURES (CONT) 
• Using a closure as a parameter type with typealias: 
typealias MyClosureType = (param1: String, param2: String) -> Void 
func myMethod(param1: String, param2: Int, closure: MyClosureType){ 
//method body 
closure(param1: "param1", param2: "param2") 
} 
Ahmed Ali
CLOSURES (CONT) 
• Calling a method that takes a closure as a parameter: 
//use either way 
myMethod("param1", param2: 4) { 
(param1, param2) -> Void in 
//closure body goes here 
} 
//or 
myMethod("param1", param2: 5, closure: { 
(param1, param2) -> Void in 
//closure body goes here 
}); 
Ahmed Ali
CLOSURES (CONT) 
• Closure capture their body references. 
• Can cause retain cycles if it refers strongly to the instance that refers strongly to it. 
• Use weak reference to break the retain cycle. 
myMethod("param1", param2: 4) { 
[weak self] (param1, param2) -> Void in 
} 
Ahmed Ali
DEMO 
Ahmed Ali

Mais conteúdo relacionado

Destaque

iOS NSAgora #3: Objective-C vs. Swift
iOS NSAgora #3: Objective-C vs. SwiftiOS NSAgora #3: Objective-C vs. Swift
iOS NSAgora #3: Objective-C vs. SwiftAlex Cristea
 
Swift as an OOP Language
Swift as an OOP LanguageSwift as an OOP Language
Swift as an OOP LanguageAhmed Ali
 
Modern Objective-C @ Pragma Night
Modern Objective-C @ Pragma NightModern Objective-C @ Pragma Night
Modern Objective-C @ Pragma NightGiuseppe Arici
 
Spring MVC to iOS and the REST
Spring MVC to iOS and the RESTSpring MVC to iOS and the REST
Spring MVC to iOS and the RESTRoy Clarkson
 
Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, SwiftYandex
 
Software architectural design patterns(MVC, MVP, MVVM, VIPER) for iOS
Software architectural design patterns(MVC, MVP, MVVM, VIPER) for iOSSoftware architectural design patterns(MVC, MVP, MVVM, VIPER) for iOS
Software architectural design patterns(MVC, MVP, MVVM, VIPER) for iOSJinkyu Kim
 
Workshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - StructuresWorkshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - StructuresVisual Engineering
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to SwiftGiordano Scalzo
 
An Introduction into the design of business using business architecture
An Introduction into the design of business using business architectureAn Introduction into the design of business using business architecture
An Introduction into the design of business using business architectureCraig Martin
 
Writing Your App Swiftly
Writing Your App SwiftlyWriting Your App Swiftly
Writing Your App SwiftlySommer Panage
 

Destaque (14)

iOS NSAgora #3: Objective-C vs. Swift
iOS NSAgora #3: Objective-C vs. SwiftiOS NSAgora #3: Objective-C vs. Swift
iOS NSAgora #3: Objective-C vs. Swift
 
Swift as an OOP Language
Swift as an OOP LanguageSwift as an OOP Language
Swift as an OOP Language
 
Swift vs Objective-C
Swift vs Objective-CSwift vs Objective-C
Swift vs Objective-C
 
Modern Objective-C @ Pragma Night
Modern Objective-C @ Pragma NightModern Objective-C @ Pragma Night
Modern Objective-C @ Pragma Night
 
Ios - Intorduction to view controller
Ios - Intorduction to view controllerIos - Intorduction to view controller
Ios - Intorduction to view controller
 
iOS: Table Views
iOS: Table ViewsiOS: Table Views
iOS: Table Views
 
Spring MVC to iOS and the REST
Spring MVC to iOS and the RESTSpring MVC to iOS and the REST
Spring MVC to iOS and the REST
 
Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, Swift
 
Software architectural design patterns(MVC, MVP, MVVM, VIPER) for iOS
Software architectural design patterns(MVC, MVP, MVVM, VIPER) for iOSSoftware architectural design patterns(MVC, MVP, MVVM, VIPER) for iOS
Software architectural design patterns(MVC, MVP, MVVM, VIPER) for iOS
 
Workshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - StructuresWorkshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - Structures
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to Swift
 
Apple iOS
Apple iOSApple iOS
Apple iOS
 
An Introduction into the design of business using business architecture
An Introduction into the design of business using business architectureAn Introduction into the design of business using business architecture
An Introduction into the design of business using business architecture
 
Writing Your App Swiftly
Writing Your App SwiftlyWriting Your App Swiftly
Writing Your App Swiftly
 

Semelhante a iOS Development using Swift: Enums, ARC, Delegation, Closures, Table View and more

iOS development using Swift - Swift Basics (2)
iOS development using Swift - Swift Basics (2)iOS development using Swift - Swift Basics (2)
iOS development using Swift - Swift Basics (2)Ahmed Ali
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05cKaz Yoshikawa
 
An introduction to functional programming with Swift
An introduction to functional programming with SwiftAn introduction to functional programming with Swift
An introduction to functional programming with SwiftFatih Nayebi, Ph.D.
 
From android/java to swift (3)
From android/java to swift (3)From android/java to swift (3)
From android/java to swift (3)allanh0526
 
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...Doris Chen
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#ANURAG SINGH
 
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan DikicInfinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan DikicInfinum
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard LibrarySantosh Rajan
 
TypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason HaffeyTypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason HaffeyRalph Johnson
 
Deep Dive Into Swift
Deep Dive Into SwiftDeep Dive Into Swift
Deep Dive Into SwiftSarath C
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants Hemantha Kulathilake
 
Exploring Koltin on Android
Exploring Koltin on AndroidExploring Koltin on Android
Exploring Koltin on AndroidDeepanshu Madan
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Meghaj Mallick
 
CSharp Advanced L05-Attributes+Reflection
CSharp Advanced L05-Attributes+ReflectionCSharp Advanced L05-Attributes+Reflection
CSharp Advanced L05-Attributes+ReflectionMohammad Shaker
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)yap_raiza
 
stacks and queues class 12 in c++
stacks and  queues class 12 in c++stacks and  queues class 12 in c++
stacks and queues class 12 in c++Khushal Mehta
 

Semelhante a iOS Development using Swift: Enums, ARC, Delegation, Closures, Table View and more (20)

iOS development using Swift - Swift Basics (2)
iOS development using Swift - Swift Basics (2)iOS development using Swift - Swift Basics (2)
iOS development using Swift - Swift Basics (2)
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05c
 
An introduction to functional programming with Swift
An introduction to functional programming with SwiftAn introduction to functional programming with Swift
An introduction to functional programming with Swift
 
From android/java to swift (3)
From android/java to swift (3)From android/java to swift (3)
From android/java to swift (3)
 
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan DikicInfinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard Library
 
TypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason HaffeyTypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason Haffey
 
Deep Dive Into Swift
Deep Dive Into SwiftDeep Dive Into Swift
Deep Dive Into Swift
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants
 
Exploring Koltin on Android
Exploring Koltin on AndroidExploring Koltin on Android
Exploring Koltin on Android
 
Aspdot
AspdotAspdot
Aspdot
 
Day 1
Day 1Day 1
Day 1
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...
 
Should i Go there
Should i Go thereShould i Go there
Should i Go there
 
Introduction to Swift 2
Introduction to Swift 2Introduction to Swift 2
Introduction to Swift 2
 
CSharp Advanced L05-Attributes+Reflection
CSharp Advanced L05-Attributes+ReflectionCSharp Advanced L05-Attributes+Reflection
CSharp Advanced L05-Attributes+Reflection
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
 
stacks and queues class 12 in c++
stacks and  queues class 12 in c++stacks and  queues class 12 in c++
stacks and queues class 12 in c++
 

Último

QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 

Último (20)

QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 

iOS Development using Swift: Enums, ARC, Delegation, Closures, Table View and more

  • 1. IOS DEVELOPMENT USING SWIFT Enums, ARC, Delegation, Closures Ahmed Ali
  • 2. TODAY TOPICS • Enumerations • Automatic Reference Count (ARC) • Delegation pattern • Closures • Demo: • Table view • Move to another screen programmatically • Pass data between screens Ahmed Ali
  • 3. ENUMERATIONS • An enumeration is a value type used to define a common type for a group of related values. • Enumeration can have methods and computed properties. • Also it can deal with numeric data types, strings, and characters. • Example: enum Direction { case UP case DOWN case LEFT case RIGHT } Ahmed Ali enum Direction : String { case UP = "Up" case DOWN = "Down" case LEFT = "Left" case RIGHT = "Right" }
  • 4. ENUMERATIONS (CONT) • There are two ways to create enum instances: • Using enum case directly. • Create instance from a raw value. This is only available if the enum type is specified. • Example: //Use direct case let up = Direction.UP //Creating from raw value. //fromRaw static method returns an optional type let down = Direction.fromRaw("Down") Ahmed Ali
  • 5. ENUMERATIONS (CONT) • Usage example: //The raw value may come from a user input or a web service response let rawDir = "Left" if let dir = Direction.fromRaw(rawDir){ //The rawDir value is one of our supported directions. Lets see which one it is switch dir{ case .UP: println("Selected direction is Up") case .DOWN: println("Selected direction is Down") default: println("Selected direction is (dir.toRaw())") } } Ahmed Ali
  • 6. ENUMERATIONS (CONT) • Enumeration in Swift are more complicated than this, they can have associated values with each case. • Example: enum Shape{ case SQUARE(width: Int, height: Int) case CIRCLE(centerX: Int, centerY: Int, radius: Int) } let shape = Shape.SQUARE(width: 50, height: 50) Ahmed Ali
  • 7. ENUMERATIONS (CONT) • Checking the value for associated value cases example: switch shape{ case .SQUARE(width: let width, height: let height): println("Square shape with dimension ((width), (height))") case .CIRCLE(20, let y, let r) where r > 5: println("Circle shape with center point ((20), (y))") case .CIRCLE(let x, let y, _): println("Circle shape with center point ((x), (y))") } Ahmed Ali
  • 8. ARC • A garbage collector is not a good solution for a limited resources devices. • Automatic Reference Count (ARC) is the memory management mechanism used widely in iOS (by Objective-C and Swift). • ARC keeps track of how many references refer to each class instance. • When these references become zero, the object’s memory space is deallocated. • ARC is only applied on class instances; structs and enums are value types, they are not stored or passed by reference. Ahmed Ali
  • 9. ARC (CONT) • Explanation example: class Order{ var orderDate : NSDate var product : Product init(orderDate: NSDate, product: Product){ self.orderDate = orderDate;self.product = product } deinit{ println("Order has been removed from the memory") } } class Product{ var name : String init(name: String){ self.name = name } deinit{ println("Product (name) has been removed from the memory") } } Ahmed Ali
  • 10. ARC (CONT) • Explanation example (Cont): var product : Product! = Product(name:"iPhone") var order : Order! = Order(orderDate: NSDate(), product: product) product = nil order = nil Ahmed Ali
  • 11. ARC – RETAIN CYCLE • All references are strong by default. • When two instances strongly refer to each other, they will remain in memory even when they are no more in use. • Your app may run out of memory and crash. Ahmed Ali
  • 12. ARC – RETAIN CYCLE (CONT) • Explanation example: class Apartment{ var tenant : Person! deinit{ println("Apartment has been removed from memory")} } class Person{ var apartment : Apartment! deinit{ println("Persone has been removed from memory")} } var apartment : Apartment! = Apartment() var tenant : Person! = Person() apartment.tenant = tenant tenant.apartment = apartment apartment = nil; tenant = nil; Ahmed Ali
  • 13. ARC – RETAIN CYCLE (CONT) • Break retain cycle example: class Apartment{ weak var tenant : Person! deinit{ println("Apartment has been removed from memory")} } class Person{ var apartment : Apartment! deinit{ println("Persone has been removed from memory")} } var apartment : Apartment! = Apartment() var tenant : Person! = Person() apartment.tenant = tenant tenant.apartment = apartment apartment = nil; tenant = nil; Ahmed Ali
  • 14. DELEGATION PATTERN • Delegation pattern allows one type to delegate some of its behaviors to an instance of another type. • A real-world example will be used in the demo. • Example: class PhotosSlideshow : UIView { func show(){} //the rest of the implmenetation } Ahmed Ali
  • 15. DELEGATION PATTERN (CONT) 1. Define a protocol which wraps the needed functionality. protocol ImagesDataSource { func numberOfImagesInSlideshow(slideShowView: PhotosSlideshow) -> UInt func slideShowView(slideShowView: PhotosSlideshow, imageAtIndex: UInt) -> UIImage } Ahmed Ali
  • 16. DELEGATION PATTERN (CONT) 1. Use this protocol as weak reference in your delegating type class PhotosSlideshow : UIView { weak var delegate : ImagesDataSource! func show(){} //the rest of the implmenetation } Ahmed Ali
  • 17. DELEGATION PATTERN (CONT) 1. Objective-c protocols allows optional requirements. @objc protocol ImagesDataSource { func numberOfImagesInSlideshow(slideShowView: PhotosSlideshow) -> UInt func slideShowView(slideShowView: PhotosSlideshow, imageAtIndex: UInt) -> UIImage optional func slideShowView(slideShowView: PhotosSlideshow, userSelectedImageAtIndex:UInt) } Ahmed Ali
  • 18. DELEGATION PATTERN (CONT) 1. Objective-c protocols allows optional requirements. class PhotosSlideshow : UIView { weak var delegate : ImagesDataSource! func show(){} func userDidSelectImage(image: UIImage) { delegate?.slideShowView?(self, userSelectedImage: image) } //the rest of the implmenetation } Ahmed Ali
  • 19. CLOSURES • A closure encapsulates a code block to be used later. • Think of it as an function without a name. • A closure can be used as any type, for example it can be used: • As the type of a property. • As a method’s parameter type. • As a return type. • Closures are passed by reference. Ahmed Ali
  • 20. CLOSURES (CONT) • Example of using Closure as a property type: var myClosure : ((param1: Int, param2: String) -> String)! • Or define the closure type with tyepalias: typealias MyClosureType = (param1: Int, param2: String) -> String var myClosure : MyClosureType! Ahmed Ali
  • 21. CLOSURES (CONT) • Assigning it a value and calling it: myClosure = { (firstParam, secondParam) -> String in println("First param: (firstParam), Second param: (secondParam)") return "(firstParam) and (secondParam)” } myClosure(param1: 4, param2: "String") Ahmed Ali
  • 22. CLOSURES (CONT) • Using a closure as a parameter type with typealias: typealias MyClosureType = (param1: String, param2: String) -> Void func myMethod(param1: String, param2: Int, closure: MyClosureType){ //method body closure(param1: "param1", param2: "param2") } Ahmed Ali
  • 23. CLOSURES (CONT) • Calling a method that takes a closure as a parameter: //use either way myMethod("param1", param2: 4) { (param1, param2) -> Void in //closure body goes here } //or myMethod("param1", param2: 5, closure: { (param1, param2) -> Void in //closure body goes here }); Ahmed Ali
  • 24. CLOSURES (CONT) • Closure capture their body references. • Can cause retain cycles if it refers strongly to the instance that refers strongly to it. • Use weak reference to break the retain cycle. myMethod("param1", param2: 4) { [weak self] (param1, param2) -> Void in } Ahmed Ali