Skip to content

Instantly share code, notes, and snippets.

View noahsark769's full-sized avatar

Noah Gilmore noahsark769

View GitHub Profile
protocol KeyPathUpdatable {}
extension KeyPathUpdatable {
func updating<LeafType>(_ keyPath: WritableKeyPath<Self, LeafType>, to value: LeafType) -> Self {
var copy = self
copy[keyPath: keyPath] = value
return copy
}
}
protocol KeyPathUpdatable {}
extension KeyPathUpdatable {
func updating<LeafType>(_ keyPath: WritableKeyPath<Self, LeafType>, to value: LeafType) -> Self {
var copy = self
copy[keyPath: keyPath] = value
return copy
}
}
@noahsark769
noahsark769 / swiftlint-example.swift
Created August 21, 2020 20:48
multiline_arguments_parameters false positives
expect(issue.createdAt).to(equal(
Helper.date(["$date": NSNumber(value: value1)])
))
expect(followers).to(containExactUnorderedElements([
Follower(
followerUid: "a",
followerType: .group
),
Follower(
@noahsark769
noahsark769 / AnalyticsController.swift
Created December 5, 2020 22:21
AnalyticsController.swift
final class AmplitudeController {
static let shared = AmplitudeController()
private let amplitudeInstance = Amplitude.instance()
private var globalProperties: [String: Any] = [:]
func initialise() {
self.amplitudeInstance.initializeApiKey("bda1f35c62f39a02dc6d4cbf416f9bb8")
self.globalProperties = [
"appVersion": AppEnvironment.version,
"buildNumber": AppEnvironment.buildNumber,
@noahsark769
noahsark769 / Appending.swift
Created May 10, 2020 21:24
Nested property wrappers with Swift
// https://noahgilmore.com/blog/nesting-property-wrappers
import Cocoa
protocol Appendable {
func appending(string: String) -> Self
}
extension String: Appendable {
func appending(string: String) -> String {
@noahsark769
noahsark769 / LoginViewController.swift
Created December 29, 2020 20:03
Copying WKWebView cookies into HTTPCookieStorage
extension LoginViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if let url = webView.url, url.absoluteString.contains("/index.action") {
let store = WKWebsiteDataStore.default().httpCookieStore
store.getAllCookies { cookies in
if let sessionIdCookie = cookies.first(where: { cookie in
cookie.name == "JSESSIONID"
}) {
HTTPCookieStorage.shared.setCookies(cookies, for: webView.url, mainDocumentURL: nil)
self.callback(ConfluenceSessionCookie(jSessonId: sessionIdCookie))
@noahsark769
noahsark769 / test.swift
Created November 29, 2019 18:51
CombineLatestCollection crash
import XCTest
import Combine
extension Collection where Element: Publisher {
/// Combine the array of publishers to give a single publisher of an array
/// of their outputs.
public func combineLatest() -> CombineLatestCollection<Self> {
return CombineLatestCollection(self)
}
import SwiftUI
// Note: There are some issues with using these modifiers inside of ButtonStyles on macOS. Please see https://twitter.com/noahsark769/status/1288256379640139776?s=20 for more info.
struct ConditionalContent<TrueContent: View, FalseContent: View>: View {
let value: Bool
let trueContent: () -> TrueContent
let falseContent: () -> FalseContent
@ViewBuilder var body: some View {
@noahsark769
noahsark769 / SwitchExpression.swift
Created January 8, 2021 17:39
SwitchExpression.swift
@_functionBuilder struct SwitchExpression {
static func buildBlock<T>(_ content: T) -> T {
return content
}
static func buildEither<T>(first: T) -> T {
return first
}
static func buildEither<T>(second: T) -> T {
// The following is a code sample based on https://twitter.com/noahsark769/status/1264681181435420672?s=20
// It's not really done, but putting it here for the benefit of the community. Maybe at some point soon I'll open source
// this into a proper library.
//
// What it does: Defines a ConstraintSystem SwiftUI view which allows you to specify autolayout constraints between views.
//
// Caveats:
// - Only works for AppKit/NSView/NSViewRepresentable, not UIKit yet
// - Only works on the first render (update(nsView) implementation is empty)
// - The constraint identifiers must be strings, it would be nice to make them generic over some type that is Hashable,