Skip to content

Instantly share code, notes, and snippets.

Avatar

Noah Gilmore noahsark769

View GitHub Profile
@noahsark769
noahsark769 / swiftlint-example.swift
Created Aug 21, 2020
multiline_arguments_parameters false positives
View swiftlint-example.swift
expect(issue.createdAt).to(equal(
Helper.date(["$date": NSNumber(value: value1)])
))
expect(followers).to(containExactUnorderedElements([
Follower(
followerUid: "a",
followerType: .group
),
Follower(
View swiftui-utils.swift
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 {
View ConstraintSystem.swift
// 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,
@noahsark769
noahsark769 / Appending.swift
Created May 10, 2020
Nested property wrappers with Swift
View Appending.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 {
View KeyPathUpdatableTwoLevelExample.swift
protocol KeyPathUpdatable {}
extension KeyPathUpdatable {
func updating<LeafType>(_ keyPath: WritableKeyPath<Self, LeafType>, to value: LeafType) -> Self {
var copy = self
copy[keyPath: keyPath] = value
return copy
}
}
View KeyPathUpdatable.swift
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 / test.swift
Created Nov 29, 2019
CombineLatestCollection crash
View test.swift
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)
}
@noahsark769
noahsark769 / README.md
Created Mar 29, 2019
Local PSQL configuration for Django projects
View README.md

I keep forgetting how to do initial local setup of postgres for django projects, so here are the steps I generally follow:

  1. Install postgres locally (for mac I like postgres.app). If you get a return value from which psql, you should be good to go
  2. Set up your local database:
$ psql
postgres=# CREATE USER myproject_user WITH PASSWORD 'myproject_user';
CREATE ROLE
postgres=# CREATE DATABASE myproject_main;
@noahsark769
noahsark769 / UIImageView+ColorWheel.swift
Created Feb 27, 2019
Get the point on a color wheel image for a given color
View UIImageView+ColorWheel.swift
extension UIImageView {
func pointOnColorWheel(for color: UIColor) -> CGPoint? {
guard let image = self.image else { return nil }
var hue: CGFloat = 0
var saturation: CGFloat = 0
var brightness: CGFloat = 0
color.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: nil)
let width = self.frame.size.width
@noahsark769
noahsark769 / UIColor+Hex.swift
Created Feb 6, 2019
Instantiate a UIColor from a hex string with 6 or 8 chars
View UIColor+Hex.swift
extension UIColor {
// Adapted from https://gist.github.com/yannickl/16f0ed38f0698d9a8ae7, modified to accept alpha
// values. Works with strings with or without "#", 6 char strings like "a735b5", 8 char strings
// with alpha value like "836be64"
convenience init(hexString: String) {
let hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
let scanner = Scanner(string: hexString)
if (hexString.hasPrefix("#")) {
scanner.scanLocation = 1
You can’t perform that action at this time.