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 { | |
/// Replace all instances of characters in a character set. | |
/// | |
/// - Parameters: | |
/// - characterSet: The `CharacterSet` containing the characters to replace in the string. | |
/// - replacementString: The `String` to replace the matched characters with. | |
/// - Returns: A new `String` with all characters in the character set replaced by the replacement string. | |
func replacingCharacters(in characterSet: CharacterSet, with replacementString: String = "") -> String { | |
return self.components(separatedBy: characterSet).joined(separator: replacementString) | |
} |
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 CLLocation { | |
/// Compass bearing from a known location | |
/// | |
/// Shamelessly stolenfrom // https://stackoverflow.com/questions/26998029/calculating-bearing-between-two-cllocation-points-in-swift | |
/// | |
/// - Parameter location: The `CLocation` to calculate the bearing from. | |
/// - Returns: The `CLLocationDirection` representing the compass bearing. | |
func bearing(from location: CLLocation) -> CLLocationDirection { | |
func degreesToRadians(degrees: CLLocationDirection) -> CLLocationDirection { return degrees * .pi / 180.0 } |
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 CLLocation: Encodable { | |
public enum CodingKeys: String, CodingKey { | |
case latitude | |
case longitude | |
case altitude | |
case horizontalAccuracy | |
case verticalAccuracy | |
case speed | |
case course | |
case timestamp |
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
/// Provides convenience accesors to some info dictionary keys | |
extension Bundle { | |
var identifier: String? { | |
return string(forInfoDictionaryKey: "CFBundleIdentifier") | |
} | |
var displayName: String? { | |
return string(forInfoDictionaryKey: "CFBundleDisplayName") | |
} | |
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 Foundation | |
public extension String { | |
// MARK: - Web URL | |
/// Extract all web urls from a string. | |
func webURLs() -> [String] { | |
guard let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) else { | |
return [] |
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
/// The Locale for a currency code. | |
/// | |
/// The returned Locale may only contain currency information. | |
/// | |
/// - Parameter currencyCode: An ISO currency code. See `Locale.commonISOCurrencyCodes` for a list of supported currency codes. | |
/// - Returns: The Locale corresponding to the currency code, or nil if its not a vakid currency code. | |
static func localeFrom(currencyCode: String) -> Locale? { | |
// Make sure the currency code is supported | |
guard self.commonISOCurrencyCodes.contains(currencyCode) else { return 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 Locale { | |
/// An emoji representation of the locale's region. | |
var emoji: String? { | |
return regionalIndicatorSymbol | |
} | |
/// The regional indicator symbols are a set of 26 alphabetic Unicode characters (A–Z) intended to be used to encode | |
/// ISO 3166-1 alpha-2 two-letter country codes in a way that allows optional special treatment. | |
/// | |
/// These were defined as part of the Unicode 6.0 support for emoji, as an alternative to encoding separate characters for each |
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 Locale { | |
/// The calling code or "country dial in code" of the locale. | |
/// See https://en.wikipedia.org/wiki/List_of_country_calling_codes#Alphabetical_listing_by_country_or_region | |
var countryCallingCode: String? { | |
return isdCode | |
} | |
/// The international subscriber dialling (ISD) code of the locale. | |
var isdCode: String? { | |
guard let regionCode = self.regionCode else { return 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
import Foundation | |
extension URL { | |
public mutating func deleteQuery() { | |
self = self.deletingQuery() | |
} | |
public func deletingQuery() -> URL { | |
guard var components = URLComponents(url: self, resolvingAgainstBaseURL: false) else { return self } | |
components.query = 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
public class AsynchronousOperation: Operation { | |
@objc private enum State: Int { | |
case ready, executing, finished | |
} | |
private var _state = State.ready | |
private let stateQueue = DispatchQueue(label: Bundle.main.bundleIdentifier! + ".op.state", attributes: .concurrent) | |
@objc private dynamic var state: State { | |
get { return stateQueue.sync { _state } } |