Skip to content

Instantly share code, notes, and snippets.

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
nbasham / CodeTimer.swift
Created April 22, 2019 23:45
A simple way to time code using Swift.
import Foundation
class CodeTimer {
var startTime: CFAbsoluteTime = 0
init() { start() }
func start() { startTime = CFAbsoluteTimeGetCurrent() }
func log(_ message: String) {
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
@nbasham
nbasham / Data+Extensions.swift
Last active January 21, 2024 14:57
Get a random date between two values. Swift 4.2+ uses Random(in:).
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)
}
@nbasham
nbasham / UIView
Last active September 5, 2017 21:49
Lets you adjust individual property of a UIView's frame. Based on the category UIView+position.m by Tyler Neylon.
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
nbasham / CloseButton.playground
Created July 31, 2017 20:01
UIButton close X Swift
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
nbasham / Sequence
Created February 18, 2017 01:05
every function tests all elements in a Swift 3 sequence. Example usage: var allOdd = [1, 5, 37].every { $0 % 2 == 1 }
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
@nbasham
nbasham / gist:c6af488949dc3435e43b3d3cba9d4df4
Last active May 8, 2017 11:08
A sizable UIViewController that can be used as a playground liveView and set to specific device sizes.
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
nbasham / StoreKitUtil.swift
Created June 22, 2016 21:10
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.
import StoreKit
public extension SKProduct {
convenience init(d : Dictionary<String, AnyObject>) {
self.init()
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
nbasham / Array
Last active February 18, 2017 00:59
Swift extension of Array. Adds every function to test if all elements meet a criteria and filterReturnIndexes that filters and returns the indexes of elements that meet criteria.
import UIKit
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
}
@nbasham
nbasham / IOSDirectoryFile
Last active May 4, 2017 15:43
Swift 3: Convenience methods to load and save an NSCoding object from the iOS directories.
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]