Skip to content

Instantly share code, notes, and snippets.

View josephlord's full-sized avatar

Joseph Lord josephlord

View GitHub Profile
@josephlord
josephlord / CSVParser.swift
Created October 1, 2021 13:12 — forked from mukeshthawani/CSVParser.swift
A simple CSV parser in Swift
/// Parses a csv string and returns a 2d array.
///
/// Size of the array will be equal to the number of rows.
/// And Size of the subarray will be equal to the
/// number of fields.
///
/// Note: Delimiter can be changed to a different character
/// like semicolon.
func parse(string: String, delimiter: Character = ",") -> [[String]]{
let rows = string.split(separator: "\n")
/// Kicks off the main loop over the async sequence. Does the main work within the for loop over the async seqence
/// - Parameters:
/// - seq: The AsyncSequence that is the source
/// - sub: The Subscriber to this Subscription
private func mainLoop(seq: AsyncSequenceType, sub: S) {
// taskHandle is kept for cancelation
taskHandle = Task {
do {
try await withTaskCancellationHandler {
Task.detached {
@josephlord
josephlord / AsyncSequencePublisherInnerActor.swift
Last active July 17, 2021 19:30
The inner actor for AsyncSequencePublisher subscription
extension PublisherAsyncSequence {
public class Iterator {
private let iActor = InnerActor()
/// Due to bug [SR-14875](https://bugs.swift.org/browse/SR-14875) we need to avoid calling continuations
/// from the actor execution context currently which is why we wrap the state in this InnerActor instead of just making the Interator
/// and actor
private actor InnerActor {
/// These typealiases are just for cleaner call sites
@josephlord
josephlord / TestAsyncFileReadPerformanceTests.swift
Created June 16, 2021 23:51
Rough and ready performance tests of AsyncSequence file reading (using
import XCTest
class TestAsyncFileReadPerformanceTests: XCTestCase {
let bundle = Bundle(for: TestAsyncFileReadPerformanceTests.self)
lazy var bigFileUrl: URL = {
bundle.url(forResource: "randomData", withExtension: "txt")!
}()
lazy var smallFileUrl: URL = {
bundle.url(forResource: "dataJun-14-2021", withExtension: "json")!
}()
@josephlord
josephlord / SceneDelegate_snippet.swift
Created June 10, 2019 23:13
An appropriate willConnectTo method to set up for multi-scene usage. State restoration needs additional work
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Use a UIHostingController as window root view controller
guard let scene = scene as? UIWindowScene else { return assertionFailure() }
let window = UIWindow(windowScene: scene)
configure(
window: window,
@josephlord
josephlord / ListContent.swift
Created June 10, 2019 23:11
Snippet of start of Core Data class (NSManagedObject subclass) with enough code to make it into a Bindable object
public class ListContent : NSManagedObject, BindableObject {
public let didChange = PassthroughSubject<Void, Never>()
private var changePublisher: PassthroughSubject<Void, Never> {
return didChange
}
override public func didChangeValue(forKey key: String) {
super.didChangeValue(forKey: key)
self.changePublisher.send(())
}
@josephlord
josephlord / FetchedResultsControllerPublisher.swift
Created June 10, 2019 23:08
Making a Combine publisher from a FetchedResultsController
//
// FetchedResultsControllerPublisher.swift
// ListsModel
//
// Created by Joseph Lord on 09/06/2019.
// Copyright © 2019 Joseph Lord. All rights reserved.
//
import Foundation
import Combine
@josephlord
josephlord / UIViewProtocolExample.swift
Created April 5, 2016 12:24
Example of requiring object to have or be an object of a certain class and conform to a protocol.
import UIKit
class MyView: UIView {}
protocol MyProtocol {
var view: UIView { get }
func myFunc()
}
extension MyProtocol {
@josephlord
josephlord / pointless swift benchmarks
Last active November 20, 2015 18:47 — forked from Catfish-Man/pointless swift benchmarks
Faster by removing variables - Original and optimised Swift (optimised is 30-40% faster
import Foundation
private let testSize = 100_000_000
private var testTime:CFAbsoluteTime = 0
private var optimisedTestTime:CFAbsoluteTime = 0
class Test
{
func testing2(var x : Int) -> Int
{
@josephlord
josephlord / GenericObjcProtocolInExtensionBroken.swift
Created September 15, 2015 10:00
Fails with "Non objc-method numberOfTableCells cannot satisy optional requirement of @objc protocol UITableViewDatasource and other similar errors when the delegate implementation is in the extension. See: https://gist.github.com/josephlord/af63ecaada47f2f2ab58 for a non-extension working version of this code.
import UIKit
class GenericTVCDelegate<A> : NSObject {
var cellCreator:A->UITableViewCell
var data:[A]
init(cellCreator:A->UITableViewCell, data:[A]) {
self.cellCreator = cellCreator
self.data = data
}
}