SlideShare a Scribd company logo
1 of 21
Download to read offline
From Android/Java to Swift (1)
Allan Shih
Agenda
● Brief language introduction
● Access control
● Optionals
● Closure
● Reference
Language
Java/Android Swift/iOS
import com.package.name; import frameworkname
int counter; var counter: Int
final String TAG = “Class Name”; let TAG = “Swfit Name”
private private
- fileprivate
default internal
protected -
- public
public open
Objects
Java/Android Swift/iOS
class Foo extends Bar {} class Foo: Bar
interface Baz {} protocol Baz
class Bar implements Baz {} class Bar: Baz
Foo() init()
void doWork(String arg) {] func doWork(arg: String) -> Void
String doWork(String arg, int type) {] func doWork(arg: String, type: Int) -> String
Foo item = new Foo(); var item = Foo()
item.doWork(arg); item.doWork(arg)
item.doWork(arg, type); item.doWork(arg, type: test)
Collection Types
Java/Android Swift/iOS
int [] someInts = new Int[10]; var someInts = [Int]()
String [] shoppingList = {“Eggs”, “Milk”}; var shoppingList = [“Eggs”, “Milk”]
Map<String, String> airports = new
HashMap<>();
var airports = [String: String]()
Map<String, String> airports =
new HashMap<>() {{
put(“YYZ”: “Toronto Pearson”);
put(“DUB”: “Dublin”)
}};
var airports = [“YYZ”: “Toronto Pearson”,
“DUB”: “Dublin” ]
airports.put(“LHR”, “London”); airports[“LHR”] = “London”
Control Flow
Java/Android Swift/iOS
for (int i = 1; i <= 5; i++) {} for index in 1...5 {}
for (int i = 1; i < 5; i++) {} for index in 1..<5 {}
for (int number: someInts) {} for number in someInts {}
do-while repeat-while
switch (type) {
case a:
doSomething(); break;
case b:
case c:
doSomething(); break;
default:
Log.d(TAG, “default”);
}
switch type {
case a:
doSomeThing()
case b, c:
doSomeThing()
default:
print(“default”)
}
Access level diagram
Class A
Extension Class A
Class B: A
Extension Class B
Module A
Module B
private
Class A
Extension Class A
Class B: A
Extension Class B
public:
Only allow
Class B and
Extension B to
use the public
class.
Class A
Extension Class A
Class B: A
Extension Class B
fileprivate:
Class A
and Extension A
in the same file.
Class A
Extension Class A
Class B: A
Extension Class B
open
Class A
Extension Class A
Class B: A
Extension Class B
internal
(Default)
Optionals
● ? - Has a value or no value at all (nil)
var surveyAnswer: String?
// surveyAnswer is automatically set to nil
let possibleString: String? = "An optional string."
let forcedString: String = possibleString!
// requires an exclamation mark
● ! - Implicitly Unwrapped Optional
let assumedString: String! = "An implicitly unwrapped optional string."
let implicitString: String = assumedString
// no need for an exclamation mark
Optional Binding
● Use optional binding to find out whether an optional contains a value,
var myString:String?
myString = "Hello, Swift!"
if let yourString = myString {
println("Your string has - (yourString)")
}else {
println("Your string does not have a value")
}
guard
● A guard statement, like an if statement, executes statements depending on
the Boolean value of an expression.
func greet(person: [String: String]) {
guard !person.isEmpty,
let name = person["name"] else {
return
}
print("Hello (name)!")
guard let location = person["location"] else {
print("I hope the weather is nice near you.")
return
}
print("I hope the weather is nice in (location).")
}
func greet(person: [String: String]) {
if !person.isEmpty {
if let name = person["name"] {
print("Hello (name)!")
if let location = person["location"] {
print("I hope the weather is nice in (location).")
} else {
print("I hope the weather is nice near you.")
}
}
}
}
Closures
● Closures in Swift are similar to that of self-contained functions organized
as blocks and called anywhere like C and Objective C languages.
● Syntax
{ (parameters) -> return type in
statements
}
Closures example
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
func backward(_ s1: String, _ s2: String) -> Bool {
return s1 > s2
}
var reversedNames = names.sorted(by: backward)
// reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
Closures example
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
var reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in
return s1 > s2
})
Inferring Type From Context
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
var reversedNames = names.sorted(by: { s1, s2 in return s1 > s2 } )
// reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
Implicit Returns from Single-Expression Closures
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
var reversedNames = names.sorted(by: { s1, s2 in s1 > s2 } )
// reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
Shorthand Argument Names
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
var reversedNames = names.sorted(by: { $0 > $1 } )
// reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
Escaping Closures
● Closures in Swift are similar to that of self-contained functions organized
as blocks and called anywhere like C and Objective C languages.
func getAppStatus(completionHandler: @escaping (IMailItem) -> Void) {
var appStatus = getAppStatusFromServer()
completionHandler(appStatus)
}
Interfaces as Callbacks (JAVA)
public interface IMailReceiveCallback {
void onMailReceive(IMailItem mailItem);
}
// AppManager.class
public static void reqGetAppStatus(IMailReceiveCallback appStatusCallback) {
IMailItem mailItem = getAppStatusFromServer();
appStatusCallback.onMailReceive(mailItem);
}
// Call method with callback.
AppManager.reqGetAppStatus(this, new IMailReceiveCallback() {
@Override
public void onMailReceive(IMailItem mailItem) {
Log.d(TAG, "app connection status: " + mailItem.getStatus());
}
});
Escaping Closures example
// AppManager.class
public static func getAppStatus(completionHandler: @escaping (IMailItem) -> Void) {
var appStatus = getAppStatusFromServer()
completionHandler(appStatus)
}
// Call method with completionHandler.
let appStatus = AppManager.getAppStatus(completionHandler: { mailItem in
print(mailItem.status)
})
Next topics
● struct and class
● enum
● interface and protocol
● extension
● multi thread
Reference
● The Swift Programming Language (Swift 3.0.1)
● Completion Handlers in Swift
● 从Java/Android到Swift iOS开发

More Related Content

What's hot

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
 
42.type: Literal-based Singleton types
42.type: Literal-based Singleton types42.type: Literal-based Singleton types
42.type: Literal-based Singleton typesGeorge Leontiev
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsBrij Kishore
 
SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.Ruslan Shevchenko
 
Scala Demystifying the Funky Stuff
Scala Demystifying the Funky StuffScala Demystifying the Funky Stuff
Scala Demystifying the Funky StuffDan Hinojosa
 
Scala for Java Developers
Scala for Java DevelopersScala for Java Developers
Scala for Java DevelopersRamnivasLaddad
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular ExpressionsMatt Casto
 
Practically Functional
Practically FunctionalPractically Functional
Practically Functionaldjspiewak
 
The ruby programming language
The ruby programming languageThe ruby programming language
The ruby programming languagepraveen0202
 
Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To ScalaPeter Maas
 
Types by Adform Research, Saulius Valatka
Types by Adform Research, Saulius ValatkaTypes by Adform Research, Saulius Valatka
Types by Adform Research, Saulius ValatkaVasil Remeniuk
 
A quick python_tour
A quick python_tourA quick python_tour
A quick python_tourcghtkh
 
Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introductionGonçalo Silva
 

What's hot (20)

From android/java to swift (3)
From android/java to swift (3)From android/java to swift (3)
From android/java to swift (3)
 
Swift Basics
Swift BasicsSwift Basics
Swift Basics
 
42.type: Literal-based Singleton types
42.type: Literal-based Singleton types42.type: Literal-based Singleton types
42.type: Literal-based Singleton types
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.
 
Intermediate JavaScript
Intermediate JavaScriptIntermediate JavaScript
Intermediate JavaScript
 
Introduction to Swift 2
Introduction to Swift 2Introduction to Swift 2
Introduction to Swift 2
 
Scala Demystifying the Funky Stuff
Scala Demystifying the Funky StuffScala Demystifying the Funky Stuff
Scala Demystifying the Funky Stuff
 
Scala for Java Developers
Scala for Java DevelopersScala for Java Developers
Scala for Java Developers
 
Scala: A brief tutorial
Scala: A brief tutorialScala: A brief tutorial
Scala: A brief tutorial
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular Expressions
 
Practically Functional
Practically FunctionalPractically Functional
Practically Functional
 
The ruby programming language
The ruby programming languageThe ruby programming language
The ruby programming language
 
Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To Scala
 
Types by Adform Research, Saulius Valatka
Types by Adform Research, Saulius ValatkaTypes by Adform Research, Saulius Valatka
Types by Adform Research, Saulius Valatka
 
Scala fundamentals
Scala fundamentalsScala fundamentals
Scala fundamentals
 
A quick python_tour
A quick python_tourA quick python_tour
A quick python_tour
 
Scala syntax analysis
Scala syntax analysisScala syntax analysis
Scala syntax analysis
 
Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introduction
 

Similar to From android/java to swift (1)

Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kirill Rozov
 
Scala in a Java 8 World
Scala in a Java 8 WorldScala in a Java 8 World
Scala in a Java 8 WorldDaniel Blyth
 
Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Gesh Markov
 
Kotlin: A pragmatic language by JetBrains
Kotlin: A pragmatic language by JetBrainsKotlin: A pragmatic language by JetBrains
Kotlin: A pragmatic language by JetBrainsJigar Gosar
 
ハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うbpstudy
 
Exploring Koltin on Android
Exploring Koltin on AndroidExploring Koltin on Android
Exploring Koltin on AndroidDeepanshu Madan
 
This Is Not Your Father's Java
This Is Not Your Father's JavaThis Is Not Your Father's Java
This Is Not Your Father's JavaSven Efftinge
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Wen-Tien Chang
 
Kotlin Programming Language. What it is all about. Roman Belov, PMM in Kotlin
Kotlin Programming Language. What it is all about. Roman Belov, PMM in KotlinKotlin Programming Language. What it is all about. Roman Belov, PMM in Kotlin
Kotlin Programming Language. What it is all about. Roman Belov, PMM in KotlinJetBrains Russia
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)HamletDRC
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP StringsAhmed Swilam
 
Intermediate Swift Language by Apple
Intermediate Swift Language by AppleIntermediate Swift Language by Apple
Intermediate Swift Language by Applejamesfeng2
 
Groovy grails types, operators, objects
Groovy grails types, operators, objectsGroovy grails types, operators, objects
Groovy grails types, operators, objectsHusain Dalal
 

Similar to From android/java to swift (1) (20)

Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
 
Scala in a Java 8 World
Scala in a Java 8 WorldScala in a Java 8 World
Scala in a Java 8 World
 
Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)
 
Kotlin: A pragmatic language by JetBrains
Kotlin: A pragmatic language by JetBrainsKotlin: A pragmatic language by JetBrains
Kotlin: A pragmatic language by JetBrains
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
ハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使う
 
Exploring Koltin on Android
Exploring Koltin on AndroidExploring Koltin on Android
Exploring Koltin on Android
 
Kotlin
KotlinKotlin
Kotlin
 
This Is Not Your Father's Java
This Is Not Your Father's JavaThis Is Not Your Father's Java
This Is Not Your Father's Java
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
 
Basic swift
Basic swiftBasic swift
Basic swift
 
Kotlin Programming Language. What it is all about. Roman Belov, PMM in Kotlin
Kotlin Programming Language. What it is all about. Roman Belov, PMM in KotlinKotlin Programming Language. What it is all about. Roman Belov, PMM in Kotlin
Kotlin Programming Language. What it is all about. Roman Belov, PMM in Kotlin
 
Groovy closures
Groovy closuresGroovy closures
Groovy closures
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Intermediate Swift Language by Apple
Intermediate Swift Language by AppleIntermediate Swift Language by Apple
Intermediate Swift Language by Apple
 
Groovy grails types, operators, objects
Groovy grails types, operators, objectsGroovy grails types, operators, objects
Groovy grails types, operators, objects
 
Benefits of Kotlin
Benefits of KotlinBenefits of Kotlin
Benefits of Kotlin
 
Kotlin Starter Pack
Kotlin Starter PackKotlin Starter Pack
Kotlin Starter Pack
 

More from allanh0526

Digital authentication
Digital authenticationDigital authentication
Digital authenticationallanh0526
 
Integration of slather and jenkins
Integration of slather and jenkinsIntegration of slather and jenkins
Integration of slather and jenkinsallanh0526
 
How to generate code coverage reports in xcode with slather
How to generate code coverage reports in xcode with slatherHow to generate code coverage reports in xcode with slather
How to generate code coverage reports in xcode with slatherallanh0526
 
Unit testing in xcode 8 with swift
Unit testing in xcode 8 with swiftUnit testing in xcode 8 with swift
Unit testing in xcode 8 with swiftallanh0526
 
Ui testing in xcode
Ui testing in xcodeUi testing in xcode
Ui testing in xcodeallanh0526
 
How to work with dates and times in swift 3
How to work with dates and times in swift 3How to work with dates and times in swift 3
How to work with dates and times in swift 3allanh0526
 
Using a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS appsUsing a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS appsallanh0526
 
iOS architecture patterns
iOS architecture patternsiOS architecture patterns
iOS architecture patternsallanh0526
 
ThingMaker in Swift
ThingMaker in SwiftThingMaker in Swift
ThingMaker in Swiftallanh0526
 
Automatic reference counting in Swift
Automatic reference counting in SwiftAutomatic reference counting in Swift
Automatic reference counting in Swiftallanh0526
 
Core data in Swfit
Core data in SwfitCore data in Swfit
Core data in Swfitallanh0526
 
From android/ java to swift (2)
From android/ java to swift (2)From android/ java to swift (2)
From android/ java to swift (2)allanh0526
 
Pipeline interface
Pipeline interfacePipeline interface
Pipeline interfaceallanh0526
 
Deploying artifacts to archiva
Deploying artifacts to archivaDeploying artifacts to archiva
Deploying artifacts to archivaallanh0526
 
Android httpclient
Android httpclientAndroid httpclient
Android httpclientallanh0526
 

More from allanh0526 (17)

Webp
WebpWebp
Webp
 
Digital authentication
Digital authenticationDigital authentication
Digital authentication
 
Integration of slather and jenkins
Integration of slather and jenkinsIntegration of slather and jenkins
Integration of slather and jenkins
 
How to generate code coverage reports in xcode with slather
How to generate code coverage reports in xcode with slatherHow to generate code coverage reports in xcode with slather
How to generate code coverage reports in xcode with slather
 
Unit testing in xcode 8 with swift
Unit testing in xcode 8 with swiftUnit testing in xcode 8 with swift
Unit testing in xcode 8 with swift
 
Ui testing in xcode
Ui testing in xcodeUi testing in xcode
Ui testing in xcode
 
How to work with dates and times in swift 3
How to work with dates and times in swift 3How to work with dates and times in swift 3
How to work with dates and times in swift 3
 
Using a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS appsUsing a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS apps
 
iOS architecture patterns
iOS architecture patternsiOS architecture patterns
iOS architecture patterns
 
ThingMaker in Swift
ThingMaker in SwiftThingMaker in Swift
ThingMaker in Swift
 
Automatic reference counting in Swift
Automatic reference counting in SwiftAutomatic reference counting in Swift
Automatic reference counting in Swift
 
Core data in Swfit
Core data in SwfitCore data in Swfit
Core data in Swfit
 
From android/ java to swift (2)
From android/ java to swift (2)From android/ java to swift (2)
From android/ java to swift (2)
 
WebRTC
WebRTCWebRTC
WebRTC
 
Pipeline interface
Pipeline interfacePipeline interface
Pipeline interface
 
Deploying artifacts to archiva
Deploying artifacts to archivaDeploying artifacts to archiva
Deploying artifacts to archiva
 
Android httpclient
Android httpclientAndroid httpclient
Android httpclient
 

Recently uploaded

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
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
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
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
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
 
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
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
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
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 

Recently uploaded (20)

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
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
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
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
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
 
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
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
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...
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 

From android/java to swift (1)

  • 1. From Android/Java to Swift (1) Allan Shih
  • 2. Agenda ● Brief language introduction ● Access control ● Optionals ● Closure ● Reference
  • 3. Language Java/Android Swift/iOS import com.package.name; import frameworkname int counter; var counter: Int final String TAG = “Class Name”; let TAG = “Swfit Name” private private - fileprivate default internal protected - - public public open
  • 4. Objects Java/Android Swift/iOS class Foo extends Bar {} class Foo: Bar interface Baz {} protocol Baz class Bar implements Baz {} class Bar: Baz Foo() init() void doWork(String arg) {] func doWork(arg: String) -> Void String doWork(String arg, int type) {] func doWork(arg: String, type: Int) -> String Foo item = new Foo(); var item = Foo() item.doWork(arg); item.doWork(arg) item.doWork(arg, type); item.doWork(arg, type: test)
  • 5. Collection Types Java/Android Swift/iOS int [] someInts = new Int[10]; var someInts = [Int]() String [] shoppingList = {“Eggs”, “Milk”}; var shoppingList = [“Eggs”, “Milk”] Map<String, String> airports = new HashMap<>(); var airports = [String: String]() Map<String, String> airports = new HashMap<>() {{ put(“YYZ”: “Toronto Pearson”); put(“DUB”: “Dublin”) }}; var airports = [“YYZ”: “Toronto Pearson”, “DUB”: “Dublin” ] airports.put(“LHR”, “London”); airports[“LHR”] = “London”
  • 6. Control Flow Java/Android Swift/iOS for (int i = 1; i <= 5; i++) {} for index in 1...5 {} for (int i = 1; i < 5; i++) {} for index in 1..<5 {} for (int number: someInts) {} for number in someInts {} do-while repeat-while switch (type) { case a: doSomething(); break; case b: case c: doSomething(); break; default: Log.d(TAG, “default”); } switch type { case a: doSomeThing() case b, c: doSomeThing() default: print(“default”) }
  • 7. Access level diagram Class A Extension Class A Class B: A Extension Class B Module A Module B private Class A Extension Class A Class B: A Extension Class B public: Only allow Class B and Extension B to use the public class. Class A Extension Class A Class B: A Extension Class B fileprivate: Class A and Extension A in the same file. Class A Extension Class A Class B: A Extension Class B open Class A Extension Class A Class B: A Extension Class B internal (Default)
  • 8. Optionals ● ? - Has a value or no value at all (nil) var surveyAnswer: String? // surveyAnswer is automatically set to nil let possibleString: String? = "An optional string." let forcedString: String = possibleString! // requires an exclamation mark ● ! - Implicitly Unwrapped Optional let assumedString: String! = "An implicitly unwrapped optional string." let implicitString: String = assumedString // no need for an exclamation mark
  • 9. Optional Binding ● Use optional binding to find out whether an optional contains a value, var myString:String? myString = "Hello, Swift!" if let yourString = myString { println("Your string has - (yourString)") }else { println("Your string does not have a value") }
  • 10. guard ● A guard statement, like an if statement, executes statements depending on the Boolean value of an expression. func greet(person: [String: String]) { guard !person.isEmpty, let name = person["name"] else { return } print("Hello (name)!") guard let location = person["location"] else { print("I hope the weather is nice near you.") return } print("I hope the weather is nice in (location).") } func greet(person: [String: String]) { if !person.isEmpty { if let name = person["name"] { print("Hello (name)!") if let location = person["location"] { print("I hope the weather is nice in (location).") } else { print("I hope the weather is nice near you.") } } } }
  • 11. Closures ● Closures in Swift are similar to that of self-contained functions organized as blocks and called anywhere like C and Objective C languages. ● Syntax { (parameters) -> return type in statements }
  • 12. Closures example let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] func backward(_ s1: String, _ s2: String) -> Bool { return s1 > s2 } var reversedNames = names.sorted(by: backward) // reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
  • 13. Closures example let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] var reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in return s1 > s2 })
  • 14. Inferring Type From Context let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] var reversedNames = names.sorted(by: { s1, s2 in return s1 > s2 } ) // reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
  • 15. Implicit Returns from Single-Expression Closures let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] var reversedNames = names.sorted(by: { s1, s2 in s1 > s2 } ) // reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
  • 16. Shorthand Argument Names let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] var reversedNames = names.sorted(by: { $0 > $1 } ) // reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
  • 17. Escaping Closures ● Closures in Swift are similar to that of self-contained functions organized as blocks and called anywhere like C and Objective C languages. func getAppStatus(completionHandler: @escaping (IMailItem) -> Void) { var appStatus = getAppStatusFromServer() completionHandler(appStatus) }
  • 18. Interfaces as Callbacks (JAVA) public interface IMailReceiveCallback { void onMailReceive(IMailItem mailItem); } // AppManager.class public static void reqGetAppStatus(IMailReceiveCallback appStatusCallback) { IMailItem mailItem = getAppStatusFromServer(); appStatusCallback.onMailReceive(mailItem); } // Call method with callback. AppManager.reqGetAppStatus(this, new IMailReceiveCallback() { @Override public void onMailReceive(IMailItem mailItem) { Log.d(TAG, "app connection status: " + mailItem.getStatus()); } });
  • 19. Escaping Closures example // AppManager.class public static func getAppStatus(completionHandler: @escaping (IMailItem) -> Void) { var appStatus = getAppStatusFromServer() completionHandler(appStatus) } // Call method with completionHandler. let appStatus = AppManager.getAppStatus(completionHandler: { mailItem in print(mailItem.status) })
  • 20. Next topics ● struct and class ● enum ● interface and protocol ● extension ● multi thread
  • 21. Reference ● The Swift Programming Language (Swift 3.0.1) ● Completion Handlers in Swift ● 从Java/Android到Swift iOS开发