konekoya
Hi, I'm Joshua, a Front-end dev from is-land system.
Joshua Lin (@konekoya)
Simply put: lambdas in other language :)
// sorted(by:) method accepts a closure that takes two arguments of the same type as the array’s contents
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
// 1. Write a function that of the correct type: (String, String) -> Bool.
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"]
// 2. Closure Expression Syntax
// { (parameters) -> return type in
// statements
// }
// for the inline closure expression, the "parameters and return type are written inside the curly braces", not outside of them.
var reversedNames = names.sorted(by: {(_ s1: String, _ s2: String) -> Bool in
return s1 > s2
})
// We write this in JS
var reversedNames = names.sort((s1: string, s2: string): boolean => {
return s1 > s2;
});
// Inferring Type From Context
var reversedNames = names.sorted(by: { $0 > $1 } )Future development of Apple platform
Support iOS 13 or later
Canvas editor
Declarative
Build App for iOS, macOS, tvOS and watchOS
Easy to use and learn
In the early development
Limit API coverage
Support iOS 2 or later
imperative
Stable, mature
Comprehensive API coverage
Only works for iOS, not other Apple platforms
By konekoya