This file contains hidden or 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 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 | |
} | |
} |
This file contains hidden or 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 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 |
This file contains hidden or 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
struct AnyKey: CodingKey { | |
var intValue: Int? | |
var stringValue: String | |
init(stringValue: String) { | |
self.stringValue = stringValue | |
self.intValue = nil | |
} | |
init?(intValue: Int) { |
This file contains hidden or 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 { | |
func withPrefix(_ prefix: String) -> String { | |
if self.hasPrefix(prefix) { return self } | |
return "\(prefix)\(self)" | |
} | |
} |
This file contains hidden or 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
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) | |
} |
This file contains hidden or 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 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) | |
} | |
} |
This file contains hidden or 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 UIKit | |
import XCTest | |
extension Collection { | |
subscript(guarded idx: Index) -> Element? { | |
indices.contains(idx) ? self[idx] : nil | |
} | |
} | |
class GuardedSubscriptTests: XCTestCase { |
This file contains hidden or 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
@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 { |
This file contains hidden or 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 UIKit | |
import XCTest | |
extension String { | |
var htmlStripped: String? { | |
data(using: .unicode) | |
.flatMap { | |
try? NSAttributedString( | |
data: $0, | |
options: [ |
This file contains hidden or 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
//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) |
OlderNewer