SlideShare a Scribd company logo
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

More Related Content

Viewers also liked

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
Alex Cristea
 
Swift as an OOP Language
Swift as an OOP LanguageSwift as an OOP Language
Swift as an OOP Language
Ahmed Ali
 
Swift vs Objective-C
Swift vs Objective-CSwift vs Objective-C
Swift vs Objective-C
Mindfire Solutions
 
Modern Objective-C @ Pragma Night
Modern Objective-C @ Pragma NightModern Objective-C @ Pragma Night
Modern Objective-C @ Pragma Night
Giuseppe Arici
 
Ios - Intorduction to view controller
Ios - Intorduction to view controllerIos - Intorduction to view controller
Ios - Intorduction to view controller
Vibrant Technologies & Computers
 
iOS: Table Views
iOS: Table ViewsiOS: Table Views
iOS: Table Views
Jussi Pohjolainen
 
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
Roy Clarkson
 
Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, Swift
Yandex
 
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
Jinkyu Kim
 
Workshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - StructuresWorkshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - Structures
Visual Engineering
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to Swift
Giordano Scalzo
 
Apple iOS
Apple iOSApple iOS
Apple iOS
Chetan Gowda
 
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
Craig Martin
 
Writing Your App Swiftly
Writing Your App SwiftlyWriting Your App Swiftly
Writing Your App Swiftly
Sommer Panage
 

Viewers also liked (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
 

Similar to 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 05c
Kaz 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 Swift
Fatih 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 Dikic
Infinum
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard Library
Santosh Rajan
 
TypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason HaffeyTypeScript Presentation - Jason Haffey
TypeScript Presentation - Jason Haffey
Ralph Johnson
 
Deep Dive Into Swift
Deep Dive Into SwiftDeep Dive Into Swift
Deep Dive Into Swift
Sarath 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 Android
Deepanshu Madan
 
Aspdot
AspdotAspdot
Day 1
Day 1Day 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...
Meghaj Mallick
 
Should i Go there
Should i Go thereShould i Go there
Should i Go there
Shimi Bandiel
 
Introduction to Swift 2
Introduction to Swift 2Introduction to Swift 2
Introduction to Swift 2
Joris Timmerman
 
CSharp Advanced L05-Attributes+Reflection
CSharp Advanced L05-Attributes+ReflectionCSharp Advanced L05-Attributes+Reflection
CSharp Advanced L05-Attributes+Reflection
Mohammad 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
 

Similar to 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++
 

Recently uploaded

“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 

Recently uploaded (20)

“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 

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