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
// Adopted from: http://stackoverflow.com/a/19976535/116862 by http://stackoverflow.com/users/2547229/benjohn. | |
import PlaygroundSupport | |
import UIKit | |
let baseFont = UIFont.preferredFont(forTextStyle: .body) | |
let baseDescriptor = baseFont.fontDescriptor | |
let proportionalFeatures = [ | |
[ |
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
// macOS 10.12.1 or iOS 10, Swift 3.0.1 | |
import Foundation | |
var calendar = Calendar(identifier: .gregorian) | |
// GMT+1 (GMT+2 under daylight saving) | |
calendar.timeZone = TimeZone(identifier: "Europe/Berlin")! | |
// 2016-10-30 02:30:00 +02:00 | |
// Europe/Berlin switched from daylight saving to winter time on this date, i.e. on 2016-10-30 03:00:00 +02:00 the clock was moved back by one hour. |
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
/// An array that keeps its elements sorted at all times. | |
public struct SortedArray<Element> { | |
// Not sure if it's a good idea to use `ArraySlice` as the backing store. It lets me make SortedArray.SubSequence == SortedArray, but the price you pay for that is that one small slice, if stored permanently and not just locally inside a function, can easily retain a much larger collection, and this is hard to notice by the developer. | |
fileprivate var _elements: ArraySlice<Element> | |
public typealias Comparator<A> = (A, A) -> Bool | |
/// The predicate that determines the array's sort order. | |
fileprivate let areInIncreasingOrder: Comparator<Element> |
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
// Parse measurement expressions like `"5 m²"` and convert them into `Measurement` values. | |
// Uses the Objective-C runtime to find the known and valid symbols for a given unit | |
// (such as `UnitArea`). Adopts `ExpressibleByStringLiteral` for easy initialization. | |
import ObjectiveC | |
enum ObjectiveCRuntime { | |
class Class { | |
let base: AnyClass |
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
/// An unescaped string from a potentially unsafe | |
/// source (such as user input) | |
struct UnsafeString { | |
var value: String | |
} | |
/// A string that either comes from a safe source | |
/// (e.g. a string literal in the source code) | |
/// or has been escaped. | |
struct SanitizedHTML { |
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 str = "👨🏾🚒" | |
print(str.unicodeScalars.map { "0x\(String($0.value, radix: 16))" }) | |
// → ["0x1f468", "0x1f3fe", "0x200d", "0x1f692"] | |
print(str.utf16.map { "0x\(String($0, radix: 16))" }) | |
// → ["0xd83d", "0xdc68", "0xd83c", "0xdffe", "0x200d", "0xd83d", "0xde92"] | |
print(str.utf16.count) | |
// → 7 | |
let utf16Offset = 2 |
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 ~= <T, U> (pattern: T.Type, value: U.Type) -> Bool { | |
return pattern == value | |
} | |
func f<T>(_ type: T.Type) { | |
switch T.self { | |
case Int.self: | |
print("Int") | |
default: | |
print("Something else") |
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
Process: Notes [17745] | |
Path: /Applications/Notes.app/Contents/MacOS/Notes | |
Identifier: com.apple.Notes | |
Version: 4.5 (863) | |
Build Info: Notes-863000000000000~1 | |
Code Type: X86-64 (Native) | |
Parent Process: ??? [1] | |
Responsible: Notes [17745] | |
User ID: 501 |
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 s = "Hello World" | |
let evenIndices = s.indices.enumerated() | |
.filter { $0.offset % 2 == 0 } | |
.map { $0.element } | |
for idx in evenIndices { | |
print(s[idx]) | |
} |
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 | |
import PlaygroundSupport | |
let bigFile = URL(string: "https://speed.hetzner.de/1GB.bin")! | |
let task = URLSession.shared.dataTask(with: bigFile) { (data, response, error) in | |
print("data: \(data)") | |
print("response: \(response)") | |
print("error: \(error)") | |
} | |
task.resume() |