Press 'Space' to continue
Press 'n' to show/hide notes
Press 's' for speaker mode
Both languages are interropable (with constraints).
We will work only on the Swift language on the following slides.
let string1 = "Hello World!"
let string2: String = "Hello World!"
var string3 = "Hello World!"
var string4: String = "Hello World!"
let array1 = ["Pierre", "Paul", "Jacques"]
let array2: [String] = ["Pierre", "Paul", "Jacques"]
var array3: [String] = [] // Empty array of strings
var array4 = [String]() // Empty array of strings
var string1: String // Mandatory, must be initialized directly or inside constructor
var string2: String? // Optional, may be nil
var string3: String! // Mandatory, must not be initialized but must not be nil when used (to prevent crash)
var string: String? = "Hello World!" // optional, even if initialized
if let string = string {
print(string) // not optional
}
// Swift 5.7 version
if let string {
print(string) // not optional
}
Guard statement is added to the common list of statements like if, else if, else ...
Its body must return something or throw an error
var string: String?
guard let string else {
print("string is nil")
return
}
print("string is not nil")
open var ... // Accessible outside and inside a module, allows to subclass and override
public var ... // Accessible outside and inside a module
internal var ... // Accessible inside a module (default, keyword not mandatory)
fileprivate var ... // Accessible inside the current file
private var ... // Accessible inside the entity and extensions inside the same file
func fooBar() // Instance Void function
func fooBar(string: String) -> Int // Instance function taking a String argument and returning an Int
static func fooBar() // Class method, no instance
class MyClass {
var value: String
init(value: String) {
self.value = value
}
}
var myClass1 = MyClass(value: "value")
var myClass2 = myClass1
myClass1.value = "new value"
print("myClass1 value: \(myClass1.value)") // new value
print("myClass2 value: \(myClass2.value)") // new value
struct MyStruct {
var value: String
}
var myStruct1 = MyStruct(value: "value")
var myStruct2 = myStruct1
myStruct1.value = "new value"
print("myStruct1 value: \(myStruct1.value)") // new value
print("myStruct2 value: \(myStruct2.value)") // value
enum Person {
case Pierre
case Paul
case Jacques
}
// With raw value
enum MyInt: Int {
case one = 1
case two
case three
}
extension String {
func appendCurrency() -> String {
self + " €"
}
}
protocol MyProtocol {
var myVar: String { get }
func doSomething()
}
struct MyStruct: MyProtocol {
var myVar: String {
"Hello World!"
}
func doSomething() {
print(myVar)
}
}
Array(1...100).map { element -> String? in
[(15, "foobar"), (3, "foo"), (5, "bar")].first(where: { element.isMultiple(of: $0.0) })?.1
}.compactMap { $0 }
// OR
Array(1...100)
.compactMap { element -> String? in [(15, "foobar"), (3, "foo"), (5, "bar")].first(where: { element.isMultiple(of: $0.0) })?.1 }
protocol DoSomething: AnyObject {
func printSomething(_ string: String)
}
class Parent: DoSomething {
func printSomething(_ str: String) {
print(str)
}
func onClick() {
Child(delegate: self).doSomething()
}
}
class Child {
weak private var delegate: DoSomething?
init(delegate: DoSomething) {
self.delegate = delegate
}
func doSomething() {
delegate?.printSomething("Hello World!")
}
}
Parent().onClick()
class Parent {
func printSomething(_ str: String) {
print(str)
}
func onClick() {
Child { [weak self] string in
self?.printSomething(string)
}.doSomething()
}
}
class Child {
private var completion: (String) -> Void
init(completion: @escaping (String) -> Void) {
self.completion = completion
}
func doSomething() {
completion("HelloWorld")
}
}
Parent().onClick()
Since Swift 5.5
func fetchImages() async throws -> [UIImage] {
// .. perform data request
}
...
let result = try await fetchImages()
Github: benjdum59
Website: benjdum59.github.io
Mail: benjamin.dumont.pro@gmail.com
Go to Homepage