Skip to content

Instantly share code, notes, and snippets.

View msewell's full-sized avatar

Michael Sewell msewell

View GitHub Profile
@msewell
msewell / SegueHandlerType.swift
Last active October 3, 2022 16:45
SegueHandlerType for Swift 3
/* The SegueHandlerType pattern, as seen on [1, 2], adapted for the changed Swift 3 syntax.
[1] https://developer.apple.com/library/content/samplecode/Lister/Listings/Lister_SegueHandlerType_swift.html
[2] https://www.natashatherobot.com/protocol-oriented-segue-identifiers-swift/
*/
protocol SegueHandlerType {
// `typealias` has been changed to `associatedtype` for Protocols in Swift 3.
associatedtype SegueIdentifier: RawRepresentable
@msewell
msewell / UIControl+ActionClosure.swift
Created January 24, 2018 16:37
UIControl.addAction(for:action:)
extension UIControl {
private class ClosureSleeve {
let closure: () -> Void
init(attachTo attachee: AnyObject, closure: @escaping () -> Void) {
self.closure = closure
objc_setAssociatedObject(attachee, "\(Int.random())", self, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
@objc
@msewell
msewell / blocks.swift
Created November 1, 2019 13:10
Random String containing of Block elements (an alternative to Lorem Ipsum?)
extension String {
private static let blockElements = (9600...9631)
.compactMap { Unicode.Scalar($0) }
.map { Character($0) }
static func blocks(_ length: Int = 8) -> String {
let choices = (1...length)
.map { _ in String.blockElements.randomElement()! }
return String(choices)
@msewell
msewell / UIColor+String.swift
Last active June 23, 2020 13:52
Generate a color from a string (deterministically)
import UIKit
extension UIColor {
/// Generate a color from the given string deterministically.
///
/// Generated colors are *not* evenly distributed in the HSL color space, but you and/or your users also probably won't be able to tell.
convenience init(_ string: String, saturation: Double = 0.8, brightness: Double = 0.8) {
let seed = Double.pi // Can be any positive irrational number. Pi was chosen for flavor.
let hash = string
.compactMap { $0.unicodeScalars.first?.value.byteSwapped }
@msewell
msewell / .swiftlint.yml
Created June 12, 2020 08:44
Generate all-inclusive .swiftlint.yml
whitelist_rules:
- anyobject_protocol
- array_init
- attributes
- block_based_kvo
- class_delegate_protocol
- closing_brace
- closure_body_length
- closure_end_indentation
- closure_parameter_position
@msewell
msewell / run-git-client-after-successful-test-run.sh
Last active June 22, 2020 10:15
Xcode behavior: run git client after successful test run
#!/bin/bash
cd $(dirname $XcodeProjectPath) # Xcode will fill $XcodeProjectPath with your project's path when run this script from an Xcode behavior
swiftlint autocorrect --config ~/Developer/.swiftlint.yml # Replace the path to your .swiftlint.yml if necessary (or leave it out entirely)
git diff-index --quiet HEAD || /usr/local/bin/stree $pwd # `stree` opens SourceTree, but can be replaced with your git client of choice
@msewell
msewell / assertDumpEqual.swift
Last active June 24, 2020 07:57
AssertEquals via `dump(_:)`. Equate the unequatable!
import func XCTest.XCTAssertEqual
/// Writes the output of `dump(_:)` into a string and returns it.
func dumpToString(_ dumpee: Any) -> String {
var string = ""
dump(dumpee, to: &string)
return string
}
@msewell
msewell / AXNameFromColor.swift
Last active October 17, 2024 12:15
AXNameFromColor: String representation of a CGColor
import Accessibility
import UIKit
extension CGColor {
var rgba: String { String(format: "R: %1.3f, G: %2.3f, B: %3.3f, A: %4.3f", components![0], components![1], components![2], components![3]) }
}
let hues: ClosedRange<Int> = (0...359)
hues // with constant saturation, brightness, and alpha
@msewell
msewell / invokeTest.swift
Last active February 17, 2021 14:42
Invoke an XCTest multiple times by overriding XCTestCase's `invokeTest()` function
// In any XCTestCase subclass:
override func invokeTest() {
let maxInvocations = 100
(1...maxInvocations).forEach {
print("Test invocation: #\($0)/\(maxInvocations)")
super.invokeTest()
}
}
// In Objective-C:
@msewell
msewell / lint-as-xcode-behavior.sh
Created March 31, 2021 07:54
Running SwiftLint as an Xcode behavior
#!/bin/bash -l
exec > /tmp/lintbehaviorlog-$(date "+%s").txt
exec 2>&1
set -x # Call this command at the top of your script and every following command will be echoed. You can turn it off with set +x
set -e # abort the script when any command exits with non-zero status
OIFS="$IFS"
IFS=$'\n'