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 CoreData | |
import Foundation | |
/// Safely copies the specified `NSPersistentStore` to a temporary file. | |
/// Useful for backups. | |
/// | |
/// - Parameter index: The index of the persistent store in the coordinator's | |
/// `persistentStores` array. Passing an index that doesn't exist will trap. | |
/// | |
/// - Returns: The URL of the backup file, wrapped in a TemporaryFile instance |
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 | |
// Playing around with MeasurementFormatter.string(from: Unit) | |
let f = MeasurementFormatter() | |
f.locale = Locale(identifier: "de_DE") | |
f.unitOptions = .naturalScale | |
f.unitStyle = .long | |
f.string(from: UnitSpeed.milesPerHour) | |
f.string(from: UnitTemperature.celsius) |
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 XCTest | |
/// An expectation that is fulfilled when a Key Value Observing (KVO) condition | |
/// is met. It's variant of `XCTKVOExpectation` with support for native Swift | |
/// key paths. | |
final class KVOExpectation: XCTestExpectation { | |
private var kvoToken: NSKeyValueObservation? | |
/// Creates an expectation that is fulfilled when a KVO change causes the | |
/// specified key path of the observed object to have an expected value. |
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() |
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
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
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
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
/// 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
// 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 |