Skip to content

Commit

Permalink
Add Swift 5.5 playground & update Enum extensions & tricks
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasAbijmil committed Nov 16, 2021
1 parent 7934850 commit f2351ca
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ enum Direction: String, Codable {
case west
}
//: ## `Enum` sans `RawValue` et `associated value`
//: Note : **Depuis Swift 5.5, ces enums sont Codable sans extra work**
enum Device: Codable {
case phone
case pad
case watch
case computer
}
//: Avant cette update, on devait faire cela en plusieurs étapes comme il suit
//: * *Step 1* : Déclaration de l'`enum`
enum Action {
case run
Expand Down Expand Up @@ -77,6 +85,13 @@ extension Action: Codable {
}
}
//: ## `Enum` avec `associated values` mais sans `RawValue`
//: Note : **Depuis Swift 5.5, ces enums sont Codable sans extra work**
enum Weather: Codable {
case sun
case wind(speed: Int)
case rain(amount: Int, chance: Int)
}
//: Avant cette update, on devait faire cela en plusieurs étapes comme il suit
//: * *Step 1* : Déclaration de l'`enum`
enum Footballeur {
case gardient(name: String)
Expand Down Expand Up @@ -133,5 +148,4 @@ extension Footballeur: Codable {
}
}
}

//: [< Previous: `for case` pattern matching](@previous)           [Home](Introduction)           [Next: `indirect enum` >](@next)
Binary file not shown.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Here's the list:
- Static properties & functions
- Strings extensions & tricks
- Swift Concurrency : `async await`, `async let` binding, `Task` & `TaskGroup`, `Actor` & `@MainActor` and more.
- Swift Updates : A file containing playgrounds for Swift updates.
- Swift 5.5
- try, throws & rethrows
- Typealias
- Unit Tests
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import Foundation
/*:
# Computed property get only – Support async throws
* Les computed properties `get` only peuvent être `async` `await`, utilisé individuellement ou ensemble
*/
//: * Exemple de `get` `throws` :
enum FileError: Error {
case missing
case unreadable
}

struct BundleFile {

let filename: String

var contents: String {
get throws {
guard let url = Bundle.main.url(forResource: filename, withExtension: nil) else {
throw FileError.missing
}

do {
return try String(contentsOf: url)
} catch {
throw FileError.unreadable
}
}
}
}
//: * Exemple de `get` `async`
struct UserService {

let username: String

var id: String {
get async {
return await fetchUserId(for: username) ?? ""
}
}

private func fetchUserId(for username: String) async -> String? { return nil }
}
//: * Exemple de `get` `async` `throws`
struct FetchService {

let url: URL

var result: Result<Data, Error> {
get async throws {
do {
let (data, _) = try await URLSession.shared.data(from: url)
return .success(data)
} catch {
return .failure(error)
}
}
}
}
//: [Home](Home)           [Next: `Double` - `CGFloat` cast >](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Foundation
import CoreGraphics
/*:
# Cast implicite Double vers CGFloat et CGFloat vers Double
* Pour rappel un `Double` est codé sur 64 bits et un `CGFloat` sur 32 bits
*/
//: * Exemple d'un `Double` converti en `CGFloat` implicitement
func foo(float: CGFloat) -> CGFloat { return float }
let doubleThree = 3.0
let fooFloat = foo(float: doubleThree)
print("Type of the arg is : \(type(of: doubleThree)) | Type of the return is \(type(of: fooFloat))")
//: * Exemple d'un `CGFloat` converti en `Double` implicitement
func foo(double: Double) -> Double { return double }
let cgFloatThree: CGFloat = 3.0
let fooDouble = foo(double: cgFloatThree)
print("Type of the arg is : \(type(of: cgFloatThree)) | Type of the return is \(type(of: fooDouble))")
//: * Remarque : Le compilateur préféreras **toujours utiliser un `Double` car il permet d'avoir un maximum de précision**
let randomDouble = Double.random(in: 0...100)
let randomCgFloat = CGFloat.random(in: 0...100)
let result = randomDouble + randomCgFloat
print("Type of the addition is : \(type(of: result))")
//: [< Previous: Computed Property `get` only](@previous)           [Home](Home)           [Next: `lazy var` in local context >](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation
/*:
# Enum non conforme à RawRepresentable peuvent être Codable sans extra work
* Avant cela on devait (voir [Enum Extensions & tricks](https://github.com/LucasAbijmil/Swift-Playgrounds/tree/master/Enum%20extensions%20%26%20tricks.playground)) :
* Déclarer une enum conforme au protocol `CodingKey`
* Déclarer une enum conforme au protocol `Error`
* Ajouter l'init pour être conforme à `Decodable`
* Ajouter la fonction `encode(to:)` pour être conforme à `Encodable`
*/
//: * Exemple d'une `Enum` sans `RawValue` et sans associated values
enum Device: Codable {
case phone
case pad
case watch
case tv
case computer
}
//: * Exemple d'une `Enum` sans `RawValue` et avec associated values
enum Weather: Codable {
case sun
case wind(speed: Int)
case rain(amount: Int, chance: Int)
}
//: [< Previous: Extending static member lookup](@previous)           [Home](Home)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import SwiftUI
/*:
# Swift peut désormais trouver des membres conformes à un protocol dans une fonction générique
* Use principalement avec SwiftUI
* Permet d'utiliser une syntaxe semblable à celle des cases d'une `Enum` pour des protocols dans une fonction générique
*/
//: * Exemple avant l'update
struct SwitchToggleStyleView: View {

var body: some View {
Toggle("Example", isOn: .constant(true))
.toggleStyle(SwitchToggleStyle())
}
}
//: * Grâce à cette évolution on peut désormais réduire le code à ceci. Noté comment la syntaxe est proche de celle d'un cas d'une enum
struct SwitchToggleStyleReducedView: View {

var body: some View {
Toggle("Example", isOn: .constant(true))
.toggleStyle(.switch)
}
}
//: [< Previous: `#if` postfix member expressions](@previous)           [Home](Home)           [Next: enum sans `RawValue` conforme à `Codable` sans extra work >](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*:
# Swift 5.5
* [Swift Concurrency](https://github.com/LucasAbijmil/Swift-Playgrounds/tree/master/Swift%20Concurrency.playground)
* [Computed Property](Computed%20Property) : Les computed properties `get` only supporte `async` & `throws` utilisé ensemble ou séparément
* [Double - CGFloat cast implicit](Double%20-%20CGFloat) : Cast implicite `CGFloat` vers `Double` et vice-versa
* [Lazy var dans un context local](Lazy%20var%20in%20Local%20Context) : `lazy var` désormais possible dans le scope de fonctions
* [#if check dans des expressions postfix](if%20postfix%20member%20expressions) : Il est désormais possible d'effectuer des `#if` checked dans des expressions postfixs
* [Protocols conformes à un protocol générique](Extending%20static%20member%20lookup) : Permet d'utiliser des protocols dans une fonction générique comme des cases d'une `Enum`
* [Enum sans RawValue conforme à Codable](Enum%20Codable%20without%20RawValue) : Les `enum` non conforme à `RawRepresentable` peuvent être conforme à `Codable` sans extra work
*/
//: La majorité des exemples sont tirés du site [What's new in Swift](https://www.whatsnewinswift.com/?from=5.4&to=5.5)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Foundation
/*:
# Lazy var dans un context local (fonction)
* Désormais les fonctions supportent les `lazy var` dans leur scope
* Voir [Lazy Property](https://github.com/LucasAbijmil/Swift-Playgrounds/tree/master/Lazy%20Property.playground)
*/
/*:
* Exemple d'une fonction avec une `lazy var`. Cette fonction print dans l'ordre :
* Before lazy
* After lazy
* In printGreeting()
* Hello, Lucas
*/
func printGreeting(to name: String) -> String {
print("In printGreeting()")
return "Hello, \(name)"
}

func lazyDummyTest() {
print("Before lazy")
lazy var name = printGreeting(to: "Lucas")
print("After lazy")
print(name)
}

lazyDummyTest()
/*:
* Exemple d'une fonction avec une `lazy var`. La variable sera calculée uniquement dans le cas où le `Bool.random()` renvoie true. La fonction print dans l'ordre :
* Before lazy
* After lazy
* Si `bool` est à true :
* In printGreeting()
* Hello, Lucas
*/
func lazyDummyBoolTest(_ bool: Bool) {
print("Before lazy")
lazy var name = printGreeting(to: "Lucas")
print("After lazy")
if bool {
print(name)
}
}

lazyDummyBoolTest(Bool.random())
//: [< Previous: `Double` - `CGFloat` cast](@previous)           [Home](Home)           [Next: `#if` postfix member expressions >](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import SwiftUI
/*:
# #if checks supportés pour les expressions postfix
* Use case principalement pour l'utilisation avec SwiftUI
*/
//: * Exemple d'un use case classique pour une `View` SwiftUI
struct SomeText: View {

var body: some View {
Text("Hello, World!")
#if os(iOS)
.font(.largeTitle)
#else
.font(.headline)
#endif
}
}
//: * Il est également possible de nester des checks
struct SomeTextWithDebug: View {

var body: some View {
Text("Hello, World!")
#if os(iOS)
.font(.largeTitle)
#if DEBUG
.foregroundColor(.red)
#endif
#else
.font(.headline)
#endif
}
}
//: [< Previous: `lazy var` in local context](@previous)           [Home](Home)           [Next: Extending static member lookup >](@next)
12 changes: 12 additions & 0 deletions Swift Updates/Swift 5.5.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='6.0' target-platform='ios' display-mode='rendered' buildActiveScheme='true' importAppTypes='true'>
<pages>
<page name='Home'/>
<page name='Computed Property'/>
<page name='Double - CGFloat'/>
<page name='Enum Codable with Associated Values'/>
<page name='Lazy var in Local Context'/>
<page name='if postfix member expressions'/>
<page name='Extending static member lookup'/>
</pages>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>Swift 5.5 (Playground).xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>Swift 5.5 (Playground).xcscheme</key>
<dict>
<key>isShown</key>
<false/>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

0 comments on commit f2351ca

Please sign in to comment.