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
| //: Playground - noun: a place where people can play | |
| import UIKit | |
| struct PriceTextProvider { | |
| let basePrice: Double | |
| let reducedPrice: Double | |
| let vat: Double |
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
| typealias Age = Int | |
| typealias Grow = Age -> Age | |
| let getOlder: Grow = { | |
| age in | |
| age.successor() | |
| } | |
| getOlder(33) |
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
| func foo(url: NSURL) { | |
| guard let (components, path) = urlGuard(url) as? (NSURLComponents, String) else { | |
| return | |
| } | |
| print("Components \(components) and path \(path)") | |
| } | |
| func urlGuard(url: NSURL) -> (NSURLComponents?, String?) { | |
| guard let components = NSURLComponents(URL: url, resolvingAgainstBaseURL: false), path = components.path else { | |
| return (nil, nil) |
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 numbers = AnySequence { () -> AnyGenerator<Int> in | |
| var i = 1 | |
| return anyGenerator { | |
| return i++ | |
| } | |
| }.lazy | |
| let fizzes = numbers.map{ $0 % 3 == 0 ? "Fizz" : "" } | |
| let buzzes = numbers.map{ $0 % 5 == 0 ? "Buzz" : "" } |
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
| for i in 1...10 where i % 2 == 0 { | |
| print("\(i) is even") | |
| } | |
| for word in ["Lorem", "ipsum", "dolor", "sit", "amet", "consectetur", | |
| "adipiscing", "elit"] where word.characters.count > 5 { | |
| print(word, "is a long word") | |
| } | |
| for i in 1...50 where i > 20 && i < 30 && (i % 2 == 0) { |
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
| func doubleToRawLongBits(num: Double) -> Int64 { | |
| var num = num | |
| var bits: Int64 = 0 | |
| memcpy(&bits, &num, sizeofValue(bits)) | |
| return bits | |
| } |
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 Collection where Index: Strideable { | |
| public func binarySearch(_ value: Iterator.Element, | |
| isOrderedBefore: (Iterator.Element, Iterator.Element) -> Bool) -> Index? { | |
| guard !isEmpty else { return nil } | |
| var left = startIndex | |
| var right = endIndex.advanced(by: -1) | |
| while left <= right { | |
| let mid = left.advanced(by: left.distance(to: right) / 2) // How does this work in Swift 4? |
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
| func toInt24(value: Int) -> NSData { | |
| var result = [UInt8]() | |
| result.append(UInt8(value >> 16)) | |
| result.append(UInt8(value >> 8)) | |
| result.append(UInt8(value)) | |
| return NSData(bytes: result, length: result.count) | |
| } | |
| func int24FromData(data: NSData, inout range: NSRange) -> Int { | |
| let offset = sizeof(UInt8) |
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 Video { | |
| let title: String | |
| let description: String? | |
| let category: String | |
| let thumbnailUrl: URL? | |
| let … | |
| } |
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 you want to follow along in a Swift Playground | |
| // import PlaygroundSupport | |
| // PlaygroundPage.current.needsIndefiniteExecution = true | |
| struct User: Codable { | |
| let id: Int | |
| let name: String | |
| let email: String | |
| } |