Skip to content

Instantly share code, notes, and snippets.

Avatar

Chris Wagner cwagdev

View GitHub Profile
View BasicFetchedResultsControllerDelegate.swift
import UIKit
import CoreData
class BasicFetchedResultsControllerDelegate: NSObject, NSFetchedResultsControllerDelegate {
let tableView: UITableView
var controllerDidChangeContentCallback: (() -> Void)?
/// The threshold for the number of inserts, deletes, and reloads to peform using animations. If the threshold is exceeded, the table will be reloaded using `reloadData()`. The default value is `50`.
var numberOfRowOperationsThresholdBeforeFullReloadOccurs = 50
View gcd.swift
// Swift 2.2
let queue = dispatch_queue_create("myqueue", nil)
dispatch_async(queue) {
// do stuff
}
// Swift 3
let queue = DispatchQueue(label: "myqueue")
queue.async {
// do stuff
@cwagdev
cwagdev / UIImageExt.swift
Created Aug 17, 2015
UIImage?(color: UIColor) initializer
View UIImageExt.swift
extension UIImage {
convenience init!(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
@cwagdev
cwagdev / CreditCard.swift
Last active Jun 27, 2019
Credit Cards represented in Swift
View CreditCard.swift
/// Describes a type of credit card for a subset of known types.
enum CreditCardType: Printable {
case Amex, DinersClub, Discover, JCB, MasterCard, Visa, Unknown
var description: String {
switch self {
case .Amex:
return "Amex"
case .DinersClub:
return "Diners Club"
@cwagdev
cwagdev / Luhn.swift
Created Jul 17, 2015
Luhn Algorithm in Swift
View Luhn.swift
func luhnCheck(number: String) -> Bool {
var sum = 0
let digitStrings = reverse(number).map { String($0) }
for tuple in enumerate(digitStrings) {
if let digit = tuple.element.toInt() {
let odd = tuple.index % 2 == 1
switch (odd, digit) {
case (true, 9):
View ValueForKeyLookupable.swift
protocol ValueForKeyLookupable {
func valueForKey(key: String) -> Any?
}
extension ValueForKeyLookupable {
func valueForKey(key: String) -> Any? {
let mirror = reflect(self)
for index in 0..<mirror.count {
let child = mirror[index]
if key == child.0 {return child.1.value}
View gist:89b1715b20b29f8959ae
func loginUserWithUsername(username: String, password: String) throws -> String {
guard username.characters.count != 0 else {
throw LoginError.EmptyUsername
}
guard password.characters.count != 0 else {
throw LoginError.EmptyPassword
}
///Handle all the other,
@cwagdev
cwagdev / UIApplicationExt.swift
Created Mar 13, 2015
UIApplication Extension for Version and App Name
View UIApplicationExt.swift
extension UIApplication {
class var versionString: String {
get {
if let bundleInfoDictionary = NSBundle.mainBundle().infoDictionary {
let buildVersionString = bundleInfoDictionary["CFBundleVersion"] as String
let marketingVersionString = bundleInfoDictionary["CFBundleShortVersionString"] as String
return marketingVersionString + " (\(buildVersionString))"
} else {
return "Unknown"
}
View gist:9de963f579ae0281b4ec

Dear Mr. Wagner,

Thank you for contacting me with your thoughts regarding net neutrality. I appreciate hearing from you on this matter.

As you know, net neutrality is the idea that internet providers and governments should treat all data on the internet equally and not discriminate based on the type of content. In 2010, the Federal Communications Commission (FCC) approved an Open Internet Order, which prohibits broadband providers from blocking or discriminating against Internet content, making net neutrality the law.

There is no question that the advancement of the Internet has spurred huge opportunities for all Americans and substantially enhanced our national economy. Access to the Internet has moved from a luxury to a necessity. As a result of the substantial increase of Internet demand, more pressure has been put on the existing infrastructure, and additional enormous investments in infrastructure are necessary in order to accommodate this ever-growing demand.

In order to ensure that we benefit fr

@cwagdev
cwagdev / A.swift
Created Aug 5, 2014
Swift - Array Extension for .any
View A.swift
extension Array {
var any: T? {
get {
if self.count > 0 {
let randomIndicie = arc4random_uniform(UInt32(self.count))
return self[Int(randomIndicie)]
} else {
return nil
}
}