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
protocol Sequence { | |
associatedtype SubSequence: Sequence | |
where Iterator.Element == SubSequence.Iterator.Element, SubSequence.SubSequence == SubSequence | |
// code | |
} |
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 Pet { | |
let name: String | |
} | |
let pets = [Pet(name: "Beethoven")] | |
let encoder = JSONEncoder() | |
try! encoder.encode(pets) |
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 array1: [String?] = ["Jon", "Snow", nil] | |
let array2: [String?] = ["Winter", "is", "coming", "!", nil] | |
array1 == array2 // false |
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
extension Array: Equatable where Element: Equatable { | |
static func ==(lhs: Array<Element>, rhs: Array<Element>) -> Bool { | |
// do stuff | |
} | |
} |
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: Codable { | |
let firstName: String | |
let lastName: String | |
} | |
let jsonData = """ | |
{ | |
"first_name": "John", | |
"last_name": "Watson" | |
} |
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 string = "Hello, World!" | |
let keyPath = \String.[string.startIndex] | |
string[keyPath: keyPath] |
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 friends = ["Ross", nil, "Joey", "Chandler"] | |
// Swift 4.0 | |
let array1 = friends.flatMap { $0 } // ["Ross", "Joey", "Chandler"] | |
// Swift 4.1 | |
let array2 = friends.compactMap { $0 } // ["Ross", "Joey", "Chandler"] |
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
// Swift 4.1 | |
#if targetEnvironment(simulator) | |
// Simulator! | |
#endif | |
// Swift 4.0 | |
#if (arch(i386) || arch(x86_64)) && os(iOS) | |
// Simulator! | |
#else | |
// Device! |
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
#if canImport(UIKit) | |
// UIKit is available! | |
import UIKit | |
#endif |
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
// Declared inside framework | |
struct Point: Hashable { | |
let x: Float | |
let y: Float | |
init(x: Float, y: Float) { | |
self.x = x | |
self.y = y | |
} | |
} |
NewerOlder