Skip to content

Instantly share code, notes, and snippets.

View bennokress's full-sized avatar

Benno Kress bennokress

View GitHub Profile
@bennokress
bennokress / CollectionExtensions.swift
Last active May 22, 2017 13:16
Extensions for Swift's Collection Type
extension Collection {
// Usage: array[optional: 2] --> returns element at position 2 if possible, else returns nil
subscript(optional i: Index) -> Iterator.Element? {
return (self.startIndex ..< self.endIndex).contains(i) ? self[i] : nil
}
}
@bennokress
bennokress / Base64ImageExtension.swift
Last active May 22, 2017 13:17
Swift Extensions to conveniently decode base64 into UIImage
import UIKit
extension String {
var image: UIImage? { return UIImage(from: self) }
}
extension UIImage {
@bennokress
bennokress / UIButtonLoop.swift
Created May 22, 2017 13:22
How to loop through all buttons in a view in Swift
for case let button as UIButton in self.view.subviews {
button.setTitleForAllStates("") // or something else
}
@bennokress
bennokress / DateExtensions.swift
Last active May 22, 2017 13:24
Extensions for Swift's Date Type
extension Date {
var isToday: Bool { return Calendar.current.isDateInToday(self) }
/// Returns a Date with the specified days added to the one it is called with
func adding(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -> Date {
var targetDay: Date
targetDay = Calendar.current.date(byAdding: .year, value: years, to: self)!
targetDay = Calendar.current.date(byAdding: .month, value: months, to: targetDay)!
targetDay = Calendar.current.date(byAdding: .day, value: days, to: targetDay)!
@bennokress
bennokress / DictionaryExtensions.swift
Created May 29, 2017 11:22
Extensions for Swift's Dictionaries
extension Dictionary where Value == Any {
// Usage: dict.value(forKey: "age", defaultValue: 100) … works with every type!
func value(forKey key: Key, defaultValue: @autoclosure () -> T) -> T {
guard let value = self[key] as? T else {
return defaultValue()
}
return value
}
@bennokress
bennokress / BoolExtensions.swift
Last active June 13, 2017 13:10
Extensions for Swift's Bool Type
extension Bool {
static func random(trueProbability: Int = 50) -> Bool {
// NEEDS THE INT EXTENSION for a random Int! --> IntExtensions.swift
return Int.random(between: 1, and: 100) <= trueProbability ? true : false
}
}
@bennokress
bennokress / ArrayExtensions.swift
Last active June 24, 2017 17:34
Extensions for Swift's Array type
extension Array {
// Usage: [1,2,3,4].shifted(by: 2) --> [3,4,1,2] or "hello".shifted(by: -1) --> "elloh"
func shifted(by shiftAmount: Int) -> Array {
guard self.count > 0, (shiftAmount % self.count) != 0 else { return self }
let moduloShiftAmount = shiftAmount % self.count
let effectiveShiftAmount = moduloShiftAmount < 0 ? moduloShiftAmount + self.count : moduloShiftAmount
let shift: (Int) -> Int = { return $0 + effectiveShiftAmount >= self.count ? $0 + effectiveShiftAmount - self.count : $0 + effectiveShiftAmount }
return self.enumerated().sorted(by: { shift($0.offset) < shift($1.offset) }).map { $0.element }
}
@bennokress
bennokress / IntExtensions.swift
Last active June 25, 2017 11:26
Extensions for Swift's Int Type
extension Int {
static func random(between min: Int, and max: Int) -> Int {
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
}
/// Returns the digits in self
var digits: Int {
return self.abs < 10 ? 1 : 1 + (self / 10).digits
}
@bennokress
bennokress / StringExtensions.swift
Last active June 25, 2017 11:47
Extensions for Swift's String Type
extension String {
/// Usage: "Hello World".until(" World") --> "Hello"
mutating func until(_ string: String) {
var components = self.components(separatedBy: string)
self = components[0]
}
/// Blows a string up to totalLength by pre- and postfixing characters (e.g. "hello".withAddedDivider("-", totalLength: 13) returns "--- hello ---")
func withAddedDivider(_ divider: Character, totalLength: Int, padding: Int = 1) -> String {
@bennokress
bennokress / Equatable.swift
Last active January 12, 2018 19:59
Extensions for Swift's Equatable Protocol
extension Equatable {
// Usage: 3.isAny(of: 0, 1, 2, 3) // true
func isAny(of candidates: Self...) -> Bool {
return candidates.contains(self)
}
}