Created
January 18, 2018 09:03
-
-
Save BasThomas/a98482aae009b6793055ff322dfb7698 to your computer and use it in GitHub Desktop.
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
import Foundation | |
struct Person: Codable { | |
public let name: String | |
} | |
let person = Person(name: "Bas") | |
let payload = try JSONEncoder().encode(person) | |
let string = String(data: payload, encoding: .utf8) // {"name":"Bas"} | |
let decodedPerson = try JSONDecoder().decode(Person.self, from: payload) // Person(name: "Bas") | |
let world = "hello, world".count // 12 | |
let family = "👨👩👧👦".count // 1 | |
let multiline = """ | |
{ | |
"hello": "world", | |
"multiple": [ | |
"one": 1, | |
"two": 2 | |
] | |
} | |
""" | |
let source = "hello, world" | |
var frequencies: [Character: Int] = [:] | |
source.map { frequencies[$0, default: 0] += 1 } | |
frequencies // ["w": 1, "r": 1, "e": 1, "o": 2, "l": 3, ",": 1, " ": 1, "h": 1, "d": 1] | |
let x = frequencies["a", default: 0] // Does not store `0` at `a`; | |
let y = frequencies["a"] ?? 0 // these lines are equivalent | |
let numbers = ["one": 1, "two": 2, "three": 3, "four": 4] | |
let strings = numbers.mapValues(String.init) // ["three": "3", "four": "4", "one": "1", "two": "2"] | |
let evens = numbers.filter { $0.value % 2 == 0 } // ["four": 4, "two": 2] | |
struct Song { | |
let artist: String | |
let name: String | |
private var _description: String { | |
return "\(artist) - \(name)" | |
} | |
} | |
extension Song: CustomStringConvertible { | |
var description: String { | |
return _description | |
} | |
} | |
Song(artist: "Rick Astley", name: "Never Gonna Give You Up") // OK! | |
var collection = ["a", "b"] | |
collection.swapAt(0, 1) // ["b", "a"] | |
protocol P {} | |
struct S: P {} | |
class C: P {} | |
let u: AnyObject & P = C() // Compiles successfully |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment