This file contains hidden or 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
| var test: Int = 1000 | |
| let data = Data(bytes: &test, count: MemoryLayout<Int>.size) | |
| var number: Int = 0 | |
| number = data.withUnsafeBytes { (pointer: UnsafePointer<Int>) -> Int in | |
| return pointer.pointee | |
| } |
This file contains hidden or 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
| class Bird { | |
| enum BiologicalSex { | |
| case undetermined | |
| case male | |
| case female | |
| } | |
| var name: String? | |
| var biologicalSex: BiologicalSex |
This file contains hidden or 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
| import Foundation | |
| struct Result { | |
| enum Level { | |
| case junior | |
| case senior | |
| case enticer | |
| case undetermined(String) | |
| } |
This file contains hidden or 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
| enum IrregularData { | |
| case string(String) | |
| case bool(Bool) | |
| case int(Int) | |
| case unknown(Any?) | |
| } | |
| extension IrregularData: Decodable { |
This file contains hidden or 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
| enum MyData { | |
| case string(String) | |
| case bool(Bool) | |
| case int(Int) | |
| case unknown | |
| } | |
| extension MyData: Decodable { |
This file contains hidden or 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
| var animals = ["Dog", "Cat", "Hamster", "Pig"] | |
| var animalTags = [3, 2, 4, 1] | |
| //Zip the two together | |
| let animalsTagsZip = zip(animals, animalTags) | |
| //Sort them based on your requirement to get an array of (String, Int) tuples | |
| let animalsTagsSorted = animalsTagsZip.sorted { (lhs, rhs) -> Bool in | |
| return lhs.1 > rhs.1 | |
| } |
This file contains hidden or 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
| let myString: String??? = "Hello, World!" | |
| print(myString!!!) | |
| if case let safelyUnwrappedString??? = myString { | |
| print(safelyUnwrappedString) | |
| } | |
| if let safelyUnwrappedString = myString as? String { | |
| print(safelyUnwrappedString) |
This file contains hidden or 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
| //Ref: https://www.klundberg.com/blog/swift-4-keypaths-and-you/ | |
| struct Person { | |
| var name: String | |
| } | |
| let aKeyPath = \Person.name | |
| type(of: aKeyPath).rootType //Root type of the keyPath: Person | |
| type(of: aKeyPath).valueType //Value type of the keyPath: String |
This file contains hidden or 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
| struct Person { | |
| var name: String | |
| var likes: [String:[Any]]? | |
| } | |
| let aKeyPath = \Person.likes?["friends"]?[0] | |
| type(of: aKeyPath).rootType | |
| type(of: aKeyPath).valueType |
This file contains hidden or 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
| enum MyError: Error { | |
| case RuntimeError(String) | |
| } | |
| func doSomething(_ completion: (Bool) throws -> Void) rethrows { | |
| // Try the completion as the closure could throw | |
| try completion(false) | |
| } | |
| do { |
OlderNewer