Skip to content

Instantly share code, notes, and snippets.

@dkw5877
dkw5877 / WaitingForConnectivity.swift
Created March 22, 2019 18:27
Example of Waiting for Connectivity on URLSession
let url = URL(string: "https://rickandmortyapi.com/api/character/")!
let config = URLSessionConfiguration.default
if #available(iOS 11, *) {
config.waitsForConnectivity = true
}
/* we keep a reference to the URLSession in case we need to invalidate */
session = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue.main)
@dkw5877
dkw5877 / Query.swift
Created March 19, 2019 15:20
Example of QUERY statement using FMDB for SQLite
/*
* @description: query the given table based on conditions provided
* @note statement example "SELECT * FROM table_name WHERE "
* @param table SQL table name to insert values into
* @param completion closure to be called when query is complete
*/
static func query(on table:String, where conditions:[WhereCondition]? = nil, completion:ResultCompletion){
var sqlStatement = "SELECT * FROM \(table)"
var values = [Any]()
@dkw5877
dkw5877 / Delete.swift
Created March 18, 2019 20:09
Example of DELETE statement using FMDB for SQLite
/*
* @description: delete a row of data from a given table based on conditions
* @note statement example "DELETE FROM table_name WHERE columnName = ? "
* @param table SQL table name to insert values into
* @param completion closure to be called when delete is complete
*/
static func delete(from table:String, where conditions:[WhereCondition], completion:Completion) {
var sqlStatement = "DELETE FROM \(table)"
@dkw5877
dkw5877 / Insert.swift
Created March 18, 2019 19:42
Example of INSERT statement using FMDB for SQLite
/*
* @description: Insert an array of values in the given table
* @warning this assumes the first field is an autoincrementing value
* @note statement example "INSERT INTO table_name VALUES (NULL, ?, ?, ?)"
* @param values array of items to insert
* @param table SQL table name to insert values into
* @param completion closure to be called when insert is complete
*/
static func insert(values: [Any], into table:String, completion:Completion) {
@dkw5877
dkw5877 / FMDBDatabase.swift
Created March 18, 2019 18:51
Example of singleton for SQLite using FMDB
class FMDBDatabase {
/*
* @description defines completion closure for database calls
*/
public typealias Completion = ((Bool, Error?)-> Void)
public typealias ResultCompletion = ((Bool, FMResultSet?, Error?)-> Void)
/*
@dkw5877
dkw5877 / Create.swift
Last active March 19, 2019 13:35
Example of CREATE statement using FMDB for SQLite
/*
* @description: creates a table with the specified list of columns and value types
* @note statement example e.g."CREATE TABLE IF NOT EXISTS table_name (Title Text, Author Text, PublicationDate Date)"
* @param table name of the table to create
* @param columns list of TableColumn values to create the columns
* @param completion closure to be called when create is complete
*/
static func create(table:String, columns:[TableColumn], completion:Completion) {
var sqlStatement = "CREATE TABLE IF NOT EXISTS \(table) (ID Integer Primary key AutoIncrement,"
@dkw5877
dkw5877 / FireworksParticleView.swift
Created March 6, 2019 21:31
An example of particle emitters used to generate fireworks
import UIKit.UIView
class FireworksParticleView:UIView {
var particleImage:UIImage?
var flareImage:UIImage?
override class var layerClass:AnyClass {
return CAEmitterLayer.self
}
@dkw5877
dkw5877 / SnowParticleView.swift
Created March 6, 2019 18:32
A UIView subclass that utilizes particle emitters to create a snow scene
import UIKit
class SnowParticleView:UIView {
var particleImage:UIImage?
override class var layerClass:AnyClass {
return CAEmitterLayer.self
}
@dkw5877
dkw5877 / lexicographicallyPrecedes.swift
Created February 12, 2019 16:20
Swift Stand Library function lexicographicallyPrecedes
@inlinable
public func lexicographicallyPrecedes<OtherSequence: Sequence>(
_ other: OtherSequence,
by areInIncreasingOrder: (Element, Element) throws -> Bool
) rethrows -> Bool
where OtherSequence.Element == Element {
var iter1 = self.makeIterator()
var iter2 = other.makeIterator()
while true {
if let e1 = iter1.next() {
@dkw5877
dkw5877 / Reverse.swift
Created February 12, 2019 15:21
Swift Standard Library Reverse function
@inlinable
public __consuming func reversed() -> [Element] {
// FIXME(performance): optimize to 1 pass? But Array(self) can be
// optimized to a memcpy() sometimes. Those cases are usually collections,
// though.
var result = Array(self)
let count = result.count
for i in 0..<count/2 {
result.swapAt(i, count - ((i + 1) as Int))
}