Skip to content

Instantly share code, notes, and snippets.

@hishma
hishma / String+Hishma.swift
Last active January 23, 2019 22:41
String extensions to make my life more convenient. When I remember them…
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)
}
@hishma
hishma / CLLocation+Hishma.swift
Created January 16, 2019 23:54
Handy CorelLocation extension. Or so I think.
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 }
@hishma
hishma / CLLocation+Encodable.swift
Created January 16, 2019 23:57
CoreLocation and Codable
extension CLLocation: Encodable {
public enum CodingKeys: String, CodingKey {
case latitude
case longitude
case altitude
case horizontalAccuracy
case verticalAccuracy
case speed
case course
case timestamp
@hishma
hishma / Bundle+Hishma.swift
Created January 16, 2019 23:59
Provides convenience accesors to some info dictionary keys
/// Provides convenience accesors to some info dictionary keys
extension Bundle {
var identifier: String? {
return string(forInfoDictionaryKey: "CFBundleIdentifier")
}
var displayName: String? {
return string(forInfoDictionaryKey: "CFBundleDisplayName")
}
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 []
/// 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 }
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
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 }
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
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 } }