Skip to content

Instantly share code, notes, and snippets.

import XCTest
extension XCTestCase {
/// Asserts that an expression throws a specific error.
///
/// - Parameters:
/// - expression: An expression that can throw an error.
/// - throws: The expected `Error`. Must conform to the `Equatable` protocol.
/// - file: The file in which failure occurred. Defaults to the file name of the test case in which this function was called.
/// - line: The line number on which failure occurred. Defaults to the line number on which this function was called.
/*
CoreDataErrors.h
Core Data
Copyright (c) 2004-2022, Apple Inc.
All rights reserved.
*/
#import <Foundation/NSObject.h>
/* NSError codes for Core Data added errors in NSCocoaErrorDomain. Foundation error codes can be found in <Foundation/FoundationErrors.h>. AppKit error codes can be found in <AppKit/AppKitErrors.h>.
@hishma
hishma / measure.swift
Last active April 5, 2019 12:37
Time a function in swift
@discardableResult
static func measure<T>(name: String = "", _ block: () -> T) -> T {
let startTime = CFAbsoluteTimeGetCurrent()
let result = block()
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
print("Time: \(name) - \(timeElapsed)")
return result
}
@hishma
hishma / updateBuildNumber.sh
Last active April 13, 2023 15:12
Shell script to update the build number (CFBundleVersion) of the info.plist in the build folder.
# Shamelessly ripped of from https://blog.curtisherbert.com/automated-xcode-build-numbers-early-2019-edition/
#
git=`sh /etc/profile; which git`
bundleVersion=`"$git" rev-list --all | wc -l | tr -d '[:space:]'`
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $bundleVersion" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
echo "☛ BUILD NUMBER: ${bundleVersion}"
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 } }
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
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 }
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
/// 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 }
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 []