Skip to content

Instantly share code, notes, and snippets.

View JasonCanCode's full-sized avatar

Jason JasonCanCode

View GitHub Profile
@JasonCanCode
JasonCanCode / ArrayValidation.swift
Last active March 20, 2017 18:26
func validateArray
func validateArray<T>(_ array: [T?]) -> [T] {
var validElements: [T] = []
for case let element? in array {
validElements.append(element)
}
return validElements
}
@JasonCanCode
JasonCanCode / SwipeControl.swift
Last active March 20, 2017 18:25
Overview slides left and/or right to reveal actions. Sliding past a threshold will trigger a set action.
import UIKit
// NOTE: percentageThreshold is shared across all instances of SwipeControl
private var percentageThreshold: Int = 75
private enum Direction {
case left, right, none
}
@JasonCanCode
JasonCanCode / NibView.swift
Last active February 22, 2018 18:55
In the xib IB, make the used custom view class (subclass of NibView) the File’s Owner and connect the view outlet to the xib view.
import UIKit
/**
In the xib IB, make the used custom view class (subclass of NibView)
the File’s Owner and connect the view outlet to the xib view.
*/
class NibView: UIView {
@IBOutlet weak private var view: UIView!
var nibName: String {
@JasonCanCode
JasonCanCode / UILabel+AttributedString.swift
Last active March 20, 2017 18:00
Assigns attributed text to the label by iterating through an array of strings with associated FontTypes.
/// Used to identify the intended format of a string when using the `attributedTextFromArray:` func of a UILabel extension.
enum FontType {
case system, bold, italic, underlined
}
extension UILabel {
/**
Assigns attributed text to the label by iterating through an array of strings with associated FontTypes.
Example use:
@JasonCanCode
JasonCanCode / UIView+Constraint.swift
Last active March 20, 2017 17:58
Checks the constraints of this view's superview, as well as all of it's subviews, for the constraint with the identifier passed.
import UIKit
extension UIView {
/// Checks the constraints of this view's superview, as well as all of it's subviews, for the constraint with the identifier passed.
func constraint(withIdentifier identifier: String) -> NSLayoutConstraint? {
if let constraint = superview?.constraintFiltered(identifier) {
return constraint
} else if let constraint = constraintFiltered(identifier) {
return constraint
@JasonCanCode
JasonCanCode / LineGraphView.swift
Created July 26, 2016 19:11
An interactive line graph
import UIKit
typealias ConnectionDrawing = Void -> Void
class LineGraphView: UIView {
var data = [Double]()
var offset: CGFloat = 0.0
var shownRange = 0 ..< 0
var maximumValueShown: Double = 0.0