-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Swift 5.5 playground & update Enum extensions & tricks
- Loading branch information
1 parent
7934850
commit f2351ca
Showing
17 changed files
with
285 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+1.29 KB
(100%)
...playground.xcworkspace/xcuserdata/lucasabijmil.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+6.04 KB
(110%)
...und/playground.xcworkspace/xcuserdata/labijmil.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
59 changes: 59 additions & 0 deletions
59
Swift Updates/Swift 5.5.playground/Pages/Computed Property.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
22 changes: 22 additions & 0 deletions
22
Swift Updates/Swift 5.5.playground/Pages/Double - CGFloat.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
24 changes: 24 additions & 0 deletions
24
.../Swift 5.5.playground/Pages/Enum Codable without RawValue.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
23 changes: 23 additions & 0 deletions
23
...Swift 5.5.playground/Pages/Extending static member lookup.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
11 changes: 11 additions & 0 deletions
11
Swift Updates/Swift 5.5.playground/Pages/Home.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
45 changes: 45 additions & 0 deletions
45
...ates/Swift 5.5.playground/Pages/Lazy var in Local Context.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
33 changes: 33 additions & 0 deletions
33
.../Swift 5.5.playground/Pages/if postfix member expressions.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
7 changes: 7 additions & 0 deletions
7
Swift Updates/Swift 5.5.playground/playground.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file added
BIN
+16.9 KB
...und/playground.xcworkspace/xcuserdata/labijmil.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
Binary file added
BIN
+26.2 KB
...playground.xcworkspace/xcuserdata/lucasabijmil.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
16 changes: 16 additions & 0 deletions
16
...s/Swift 5.5.playground/xcuserdata/labijmil.xcuserdatad/xcschemes/xcschememanagement.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
16 changes: 16 additions & 0 deletions
16
...ift 5.5.playground/xcuserdata/lucasabijmil.xcuserdatad/xcschemes/xcschememanagement.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |