Skip to content

Instantly share code, notes, and snippets.

View danielctull's full-sized avatar

Daniel Tull danielctull

View GitHub Profile
@danielctull
danielctull / GreatWesternTrailRandomTile.swift
Created April 27, 2017 10:10
A random generator for private building tiles in Great Western Trail.
import Foundation
extension String {
func pad(to length: Int) -> String {
guard self.characters.count < length else {
return self
}
@danielctull
danielctull / ReplaceSegue.swift
Created March 7, 2017 10:15
A simple NSStoryboardSegue to replace the last view controller in a NSSplitViewController.
import Cocoa
class ReplaceSegue: NSStoryboardSegue {
override func perform() {
guard
let source = sourceController as? NSViewController,
let new = destinationController as? NSViewController,
// Used to make sure the value is 0 or above
public struct Positive {
let value: Float
public init(_ value: Float) {
if value < 0 {
self.value = 0
@danielctull
danielctull / AppDelegate.m
Last active April 12, 2019 07:15
Shows selection of table view cells on pressing the up/down arrow keys.
@implementation AppDelegate
#pragma mark - Key Command Controller
- (KeyCommandController *)keyCommandController {
if (!_keyCommandController) {
UIViewController *rootViewController = self.window.rootViewController;
if (rootViewController){
import Foundation
protocol File {}
class FileClass: File {}
let set = NSOrderedSet(array: [FileClass(), FileClass()])
let files = set.array as? [File] // Type 'File' does not conform to protocol 'AnyObject'

Keybase proof

I hereby claim:

  • I am danielctull on github.
  • I am danielctull (https://keybase.io/danielctull) on keybase.
  • I have a public key ASCl7emory6aN12AhnRtqg-o8bSLZcWLJFdPl1KLO0Vcigo

To claim this, I am signing this object:

@danielctull
danielctull / CTFrame.swift
Last active April 13, 2019 17:42
Is there a better way of coercing CFArray to a swift array?
import CoreText
extension CTFrame {
var lines: [CTLine] {
// This is the only way I've currently found to get the
// CFArray of CTLines to cast to a swift array.
let linesAO: [AnyObject] = CTFrameGetLines(self) as [AnyObject]
var images: [UIImage] = []
for i in 0...9 {
guard let image = UIImage(named: "Loading \(i)") else {
continue
}
images.append(image)
}
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += " a prime number, and also"
}
description += " an integer."
@danielctull
danielctull / condensed-pure.swift
Last active August 29, 2015 14:28
Using Optional.Some() to condense two guard statements into one.
var phone: String? {
guard
let phone = phoneNumberTextField?.text,
let phoneDetector = try? NSDataDetector(types: NSTextCheckingType.PhoneNumber.rawValue),
let range = pure(NSRange(location: 0, length: phone.startIndex.distanceTo(phone.endIndex))),
let matches = pure(phoneDetector.matchesInString(phone, options: [], range: range))
where matches.count > 0
else {
return nil