Skip to content

Instantly share code, notes, and snippets.

nbasham / UIColor.swift
Last active Sep 9, 2022
Swift 4: Convert CSS color names and RGB hex values to UIColor. UIColor from hex 3, 4, 6, and 8 characters in length with or without # prefix. UIColor to hex. UIColor extension that creates immutable UIColor instances from hexadecimal and CSS color name strings (e.g. ff0, #f00, ff0000, ff0000ff, Pink, aZure, CLEAR, nil). Conversely, you can obta…
View UIColor.swift
// UIColor.swift
// previously Color+HexAndCSSColorNames.swift
// Created by Norman Basham on 12/8/15.
// Copyright ©2018 Black Labs. All rights reserved.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
nbasham / Data+Extensions.swift
Last active Mar 20, 2022
Get a random date between two values. Swift 4.2+ uses Random(in:).
View Data+Extensions.swift
import Foundation
extension Date {
static func randomBetween(start: String, end: String, format: String = "yyyy-MM-dd") -> String {
let date1 = Date.parse(start, format: format)
let date2 = Date.parse(end, format: format)
return Date.randomBetween(start: date1, end: date2).dateString(format)
View gist:58802c851a8269c818707fc63f29cb54
import SwiftUI
public enum Enum1: Int, Codable, CaseIterable, CustomStringConvertible {
public var description: String {
return "\(rawValue)"
case one, two, three
public enum Enum2: String, Codable, CaseIterable, CustomStringConvertible {
nbasham / StoreKitUtil.swift
Created Jun 22, 2016
Enables creation of SKProduct and SKPaymentTransaction so StoreKit work flow and model can be the same on a simulator as it is on a device.
View StoreKitUtil.swift
import StoreKit
public extension SKProduct {
convenience init(d : Dictionary<String, AnyObject>) {
self.setValue(d["uid"], forKey: "productIdentifier")
self.setValue(d["title"], forKey: "localizedTitle")
self.setValue(d["detail"], forKey: "localizedDescription")
self.setValue(NSDecimalNumber(string: (d["price"] as! String?)), forKey: "price")
nbasham / CodeTimer.swift
Created Apr 22, 2019
A simple way to time code using Swift.
View CodeTimer.swift
import Foundation
class CodeTimer {
var startTime: CFAbsoluteTime = 0
init() { start() }
func start() { startTime = CFAbsoluteTimeGetCurrent() }
func log(_ message: String) {
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
nbasham / UIView
Last active Sep 5, 2017
Lets you adjust individual property of a UIView's frame. Based on the category UIView+position.m by Tyler Neylon.
View UIView
import UIKit
public extension UIView {
public var top: CGFloat {
get { return self.frame.origin.y }
set { self.frame.origin.y = newValue }
public var left: CGFloat {
get { return self.frame.origin.x }
nbasham / CloseButton.playground
Created Jul 31, 2017
UIButton close X Swift
View CloseButton.playground
import UIKit
import PlaygroundSupport
func getCloseButton(frame: CGRect, color: UIColor) -> UIButton? {
guard frame.size.width == frame.size.height else { return nil }
let button = UIButton(type: .custom)
button.frame = frame
button.setTitleColor(color, for: .normal)
button.setTitle("X", for: .normal)
button.layer.borderColor = color.cgColor
nbasham / gist:c6af488949dc3435e43b3d3cba9d4df4
Last active May 8, 2017
A sizable UIViewController that can be used as a playground liveView and set to specific device sizes.
View gist:c6af488949dc3435e43b3d3cba9d4df4
import UIKit
import PlaygroundSupport
public class DeviceViewController : UIViewController {
public enum ScreenType : Int {
case iPhone3_5Inch
case iPhone4Inch // includes 5th & 6h gen iPod Touch
case iPhone4_7Inch
case iPhone5_5Inch
nbasham / IOSDirectoryFile
Last active May 4, 2017
Swift 3: Convenience methods to load and save an NSCoding object from the iOS directories.
View IOSDirectoryFile
import Foundation
protocol IOSDirectoryFile {
static var dirSelector: FileManager.SearchPathDirectory { get }
extension IOSDirectoryFile {
static var url: URL {
return FileManager.default.urls(for: dirSelector, in: .userDomainMask)[0]
nbasham / Sequence
Created Feb 18, 2017
every function tests all elements in a Swift 3 sequence. Example usage: var allOdd = [1, 5, 37].every { $0 % 2 == 1 }
View Sequence
public extension Sequence {
// return true if all elements return true
func every<T>(predicate:(T) -> Bool) -> Bool {
for item in self {
if !predicate(item as! T) {
return false
return true