This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension String { | |
var image: UIImage? { return UIImage(from: self) } | |
} | |
extension UIImage { | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Equatable { | |
// Usage: 3.isAny(of: 0, 1, 2, 3) // true | |
func isAny(of candidates: Self...) -> Bool { | |
return candidates.contains(self) | |
} | |
} | |
OlderNewer