This file contains 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 | |
public extension UIColor { | |
convenience init(hex: String) { | |
let r, g, b, a: CGFloat | |
var hex = hex | |
if hex.hasPrefix("#") { hex = String(hex.dropFirst()) } | |
if hex.count == 6 { |
This file contains 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 Foundation | |
import XcodeKit | |
class SourceEditorCommand: NSObject, XCSourceEditorCommand { | |
// Reverses code on selected lines | |
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void ) -> Void { | |
let lines = invocation.buffer.lines as? [String] ?? [] |
This file contains 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
let data = """ | |
{ | |
"response": [ | |
{ | |
"name": "Kitty", | |
}, | |
{ | |
"name": "Doggy", | |
} | |
] |
This file contains 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
let exampleData = """ | |
[ | |
{ | |
"type": "cat", | |
"name": "Kitty", | |
}, | |
{ | |
"type": "dog", | |
"name": "Doggy", | |
} |
This file contains 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 LossyArray<Element: Decodable>: Decodable { | |
private(set) var elements: [Element] | |
init(from decoder: Decoder) throws { | |
var container = try decoder.unkeyedContainer() | |
var elements = [Element]() | |
if let count = container.count { | |
elements.reserveCapacity(count) | |
} |
This file contains 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 AnimalContainer: Decodable { | |
let animal: Animal | |
enum AnimalType: String, Decodable { | |
case cat | |
case dog | |
} | |
enum CodingKeys: String, CodingKey { | |
case type |
This file contains 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 UserSettings: Codable { | |
let someBool: Bool | |
let someDate: Date | |
enum CodingKeys: String, CodingKey { | |
case someBool = "someDifferentKeyBool" | |
case someDate = "someDifferentKeyDate" | |
} | |
} |
This file contains 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
e |
This file contains 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 URLSession { | |
func send<T: Decodable>(_ request: URLRequest, | |
completion: @escaping ((Result<T, Error>) -> Void)) { | |
let task = dataTask(with: request) { (data, response, error) in | |
if let data = data { | |
do { | |
let decoded = try JSONDecoder().decode(T.self, from: data) | |
completion(.success(decoded)) | |
} | |
catch let error { |