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 emailRegex = /((?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*"))@((?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]))/ | |
| let redactText = "…" | |
| let maxUserNameLength = 24 | |
| func redactEmail(for line: String) -> String { | |
| line.replacing(emailRegex) { match in | |
| let (_, name, domain) = match.output | |
| // If the username is too long, it might be a false positive for an email. | |
| return name.count > maxUserNameLength ? "\(name)@\(domain)" : redactText |
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
| @discardableResult | |
| public func send(_ action: Action) async throws -> State { | |
| try Task.checkCancellation() | |
| let effect = await reducer.reduce(into: &viewState, action: action) | |
| if let nextAction = await effect.run() { | |
| return try await send(nextAction) | |
| } | |
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
| class ViewController: UIViewController { | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| let label1 = addLabel(with: "I am a dynamic object") | |
| let spacer1 = addSpacer() | |
| let label2 = addLabel(with: "Me too.") | |
| let spacer2 = addSpacer() | |
| let label3 = addLabel(with: "Me three") | |
| let spacer3 = addSpacer() |
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
| Task { | |
| await withDiscardingTaskGroup { group in // or, `withTaskGroup(of: Void.self) { group in … }` | |
| for destination in pingResults.keys { | |
| let hostname = destination.hostname | |
| group.addTask { [self] in | |
| do { | |
| let averagePing = try await getAveragePing(hostname: hostname, interval: 1, timeout: 2, attempts: 5) | |
| pingResults[destination] = .success(averagePing) | |
| processingFirstResult = 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
| var task: Task<Void, Error>? | |
| let (stream, continuation) = AsyncStream.makeStream(of: URL.self) | |
| task = Task { | |
| for await url in stream { | |
| do { | |
| print("Sending request to \(url)") | |
| let (data, response) = try await URLSession.shared.data(from: url) | |
| // do something with data & response |
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
| class ViewController: NSViewController { | |
| let calendar = Calendar.autoupdatingCurrent | |
| var previousIdentifier: String = Calendar.current.timeZone.identifier | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| // poll to see if the identifier changed | |
| Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] timer in |
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 | |
| class ViewController: NSViewController { | |
| private let scrollView = NSScrollView() | |
| private let tableView = NSTableView() | |
| private let arrayController = NSArrayController() | |
| private var observers: [NSKeyValueObservation] = [] | |
| // Your data source array | |
| @objc dynamic var people: [Person] = [ |
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 UIKit | |
| class HandleView: UIView { | |
| var corner: Corner = .unknown | |
| } | |
| extension HandleView { | |
| enum Corner: Int { | |
| case unknown = -1 | |
| case topLeft = 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
| var valuesA: [Float] = [2, 0.5, 9, 1, 18] | |
| let rowsA = Int32(valuesA.count) | |
| let columnsA = Int32(valuesA.count) | |
| var rowIndicesA = Array(0 ..< rowsA) | |
| var columnStarts = Array(0 ... valuesA.count) | |
| var valuesB: [Float] = [3, 5, 8, 7, 2, | |
| 1, 4, 9, 1, 3, | |
| 2, 7, 3, 1, 2, | |
| 5, 4, 9, 1, 6, |
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 fetchData<M: Decodable>(target: T, responseClass: M.Type = M.self, completion: @escaping(Result<M, Error>) -> Void) { | |
| let method = Alamofire.HTTPMethod(rawValue: target.methods.rawValue) | |
| let headers = Alamofire.HTTPHeaders(target.headers ?? [:]) | |
| let params = buildParams(task: target.task) | |
| AF.request(target.baseUrl + target.path, method: method, parameters: params.0, encoding: params.1, headers: headers) | |
| .validate() | |
| .responseDecodable(of: M.self) { response in | |
| switch response.result { | |
| case .success(let value): completion(.success(value)) | |
| case .failure(let error): completion(.failure(error)) |