This file contains 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
/* 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 |
This file contains 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
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 |
This file contains 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
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) |
This file contains 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 | |
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 } |
This file contains 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
whitelist_rules: | |
- anyobject_protocol | |
- array_init | |
- attributes | |
- block_based_kvo | |
- class_delegate_protocol | |
- closing_brace | |
- closure_body_length | |
- closure_end_indentation | |
- closure_parameter_position |
This file contains 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
#!/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 |
This file contains 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 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 | |
} |
This file contains 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 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 |
This file contains 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
// In any XCTestCase subclass: | |
override func invokeTest() { | |
let maxInvocations = 100 | |
(1...maxInvocations).forEach { | |
print("Test invocation: #\($0)/\(maxInvocations)") | |
super.invokeTest() | |
} | |
} | |
// In Objective-C: |
This file contains 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
#!/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' |
OlderNewer