Skip to content

Instantly share code, notes, and snippets.

@stinger
stinger / CombineFetcher.swift
Last active January 28, 2023 18:07
Combine - fetching data using URLSession publishers
import Foundation
import Combine
enum APIError: Error, LocalizedError {
case unknown, apiError(reason: String)
var errorDescription: String? {
switch self {
case .unknown:
return "Unknown error"
@marcetcheverry
marcetcheverry / String.swift
Last active November 6, 2023 20:22
Convert HTML to Text/String in Swift
extension String {
/// Using regular expressions is not a correct approach for converting HTML to text, there are many pitfalls, like handling <style> and <script> tags. On platforms that support Foundation, one alternative is to use NSAttributedString's basic HTML support. Care must be taken to handle extraneous newlines and object replacement characters left over from the conversion process. It is a good idea to cache complex generated NSAttributedStrings either through storage or NSCache.
func strippingHTML() throws -> String? {
if isEmpty {
return nil
}
if let data = data(using: .utf8) {
let attributedString = try NSAttributedString(data: data,
options: [.documentType : NSAttributedString.DocumentType.html,
@ethanhuang13
ethanhuang13 / .swiftlint.yml
Last active April 18, 2019 10:07
.swiftlint.yml
# Updated for SwiftLint 0.29.2
# See all rules: https://github.com/realm/SwiftLint/blob/master/Rules.md
opt_in_rules:
# Opt-in (Default Disabled)
- anyobject_protocol
- array_init
- attributes
- closure_body_length
- closure_spacing
@fxm90
fxm90 / NotificationTestCase.swift
Last active April 8, 2020 12:58
XCTest - Assert notification (not) triggered.
import XCTest
class NotificationTestCase: XCTestCase {
func testTriggerNotification() {
expectation(forNotification: .fooBar,
object: nil,
handler: nil)
let notificationCenter = NotificationCenter.default
import Foundation
import Gridicons
import UIKit
import WebKit
class WebViewController: UIViewController {
let webView = WKWebView()
let progressView = WebProgressView()
let toolbar = UIToolbar()
let titleView = NavigationTitleView()
@myshov
myshov / function_invocation.js
Last active August 19, 2024 12:23
11 Ways to Invoke a Function
console.log(1);
(_ => console.log(2))();
eval('console.log(3);');
console.log.call(null, 4);
console.log.apply(null, [5]);
new Function('console.log(6)')();
Reflect.apply(console.log, null, [7])
Reflect.construct(function(){console.log(8)}, []);
Function.prototype.apply.call(console.log, null, [9]);
Function.prototype.call.call(console.log, null, 10);
@donpark
donpark / NotificationEnum.swift
Created December 2, 2016 18:17
How to use enum of Notification.Name
import Foundation
// Extension allowing enum of Notification.Names.
#if swift(>=3.0)
extension Notification.Name: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
@tadija
tadija / FontNames-iOS-17.4.swift
Last active June 26, 2025 20:25
iOS - All Font Names
/*
*** Academy Engraved LET ***
AcademyEngravedLetPlain
---------------------
*** Al Nile ***
AlNile
AlNile-Bold
---------------------
*** American Typewriter ***
AmericanTypewriter
import UIKit
extension UIImage {
// colorize image with given tint color
// this is similar to Photoshop's "Color" layer blend mode
// this is perfect for non-greyscale source images, and images that have both highlights and shadows that should be preserved
// white will stay white and black will stay black as the lightness of the image is preserved
func tint(tintColor: UIColor) -> UIImage {
@bendodson
bendodson / RandomColorWithSeed (Swift 3)
Last active January 25, 2024 17:05
Generate a random color with a seed string using Swift 3
import UIKit
func randomColor(seed: String) -> UIColor {
var total: Int = 0
for u in seed.unicodeScalars {
total += Int(UInt32(u))
}