Skip to content

Instantly share code, notes, and snippets.

View atierian's full-sized avatar

Ian Saultz atierian

  • Amazon Web Services
  • Charleston, SC
View GitHub Profile
@atierian
atierian / Data+PrettyPrinted.swift
Last active November 30, 2020 00:48
Extension on Data to pretty print in debug console
extension Data {
var prettyPrintedJSON: NSString? {
guard let object = try? JSONSerialization.jsonObject(with: self, options: []),
let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted, .withoutEscapingSlashes]),
let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return nil }
return prettyPrintedString
}
}
@atierian
atierian / UIImageView+URL.swift
Last active November 30, 2020 00:35
Simple extension on UIImageView to display UIImage from URL
extension UIImageView {
func load(url: URL?) {
DispatchQueue.global().async { [weak self] in
guard let image = url
.flatMap({ try? Data(contentsOf: $0) })
.flatMap({ UIImage(data: $0) })
else { return }
DispatchQueue.main.async {
self?.image = image
@atierian
atierian / AnyKey.swift
Last active September 4, 2020 10:41
Custom encoding strategy to swap CodingKeys during encoding
struct AnyKey: CodingKey {
var intValue: Int?
var stringValue: String
init(stringValue: String) {
self.stringValue = stringValue
self.intValue = nil
}
init?(intValue: Int) {
@atierian
atierian / String+Prefix.swift
Created August 28, 2020 01:19
Add prefix to String if not already present (taken from hackingwithswift.com)
extension String {
func withPrefix(_ prefix: String) -> String {
if self.hasPrefix(prefix) { return self }
return "\(prefix)\(self)"
}
}
@atierian
atierian / Codable+UserDefaults.swift
Created August 28, 2020 01:28
Store and retrieve Codable objects in UserDefaults
func object<T: Codable>(_ type: T.Type, with key: String, usingDecoder decoder: JSONDecoder = JSONDecoder()) -> T? {
guard let data = value(forKey: key) as? Data else { return nil }
return try? decoder.decode(type.self, from: data)
}
func set<T: Codable>(object: T, forKey key: String, usingEncoder encoder: JSONEncoder = JSONEncoder()) {
let data = try? encoder.encode(object)
set(data, forKey: key)
}
@atierian
atierian / mirrorPrint.swift
Created August 31, 2020 00:41
Debug print all properties in an object in foo = bar form
public func mirrorPrint(_ object: Any, terminator: String = "\n") {
for child in Mirror(reflecting: object).children {
guard let label = child.label else {
Swift.print(child.value, terminator: terminator)
continue
}
Swift.print(label, "=", child.value, terminator: terminator)
}
}
@atierian
atierian / Array+GuardedSubscript.swift
Last active July 21, 2021 13:29
Extension on subscript of Array to return nil if subscript is out of range
import UIKit
import XCTest
extension Collection {
subscript(guarded idx: Index) -> Element? {
indices.contains(idx) ? self[idx] : nil
}
}
class GuardedSubscriptTests: XCTestCase {
@propertyWrapper
struct UserDefault<Value> {
let key: String
let defaultValue: Value
var wrappedValue: Value {
get {
(UserDefaults.standard.object(forKey: self.key) as? Value) ?? self.defaultValue
}
set {
@atierian
atierian / String+HTMLStripped.swift
Last active June 24, 2021 16:23
Strip HTML tags / Decode HTML from String
import UIKit
import XCTest
extension String {
var htmlStripped: String? {
data(using: .unicode)
.flatMap {
try? NSAttributedString(
data: $0,
options: [
@atierian
atierian / String+StrikeThrough.swift
Last active December 23, 2020 14:21
Strike through String while maintaining String type
//Doesn't work with all characters - use cautiously. NSAttributedString is preferred.
extension String {
var strikeThrough: String {
guard let strikeThrough = "\u{0336}".unicodeScalars.first else {
return ""
}
return self.map { char -> String in
var c = UnicodeScalarView(char.unicodeScalars)
c.append(strikeThrough)
return String(c)