Skip to content

Instantly share code, notes, and snippets.

@nbasham
nbasham / ThreadUtil.swift
Last active January 29, 2016 23:21
Run a block on the main thread after a specified number of seconds.
import Foundation
public class ThreadUtil : NSObject {
// Run a block on the main thread after a specified number of seconds, thanks raywenderlich.com for splaining it so well
public static func runOnMainThreadAfterDelay(delaySeconds: Double, closure: () -> ()) {
let startTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delaySeconds * Double(NSEC_PER_SEC)))
dispatch_after(startTime, dispatch_get_main_queue(), closure)
}
@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 / 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 / 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]
@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 / 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 / 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 / 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")
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 / 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