Skip to content

Instantly share code, notes, and snippets.

func encrypt(_ text: String) -> String? {
guard
let bundle = Bundle(identifier: "com.example.bundle.identifier"),
let url = bundle.url(forResource: "certificate", withExtension: "der"),
let certData = try? Data(contentsOf: url),
let cert = SecCertificateCreateWithData(nil, certData as CFData) else {
print("Couldn't find/create pinned certificate")
return nil
}
var trust: SecTrust?
@alfavata
alfavata / Protocols.swift
Last active August 5, 2020 13:14
Protocols, inheritance, default implementations, and optional methods
import Foundation
protocol P {
var required: String { get }
}
extension P {
var required: String { "Default implementation of `required`" }
var nonRequired: String { "Default implementation of `nonRequired`" }
}
@alfavata
alfavata / RawRepresentable.swift
Last active August 5, 2020 13:12
Failable Codable initialiser for RawRepresentable types
extension RawRepresentable where RawValue: Decodable {
init?(from decoder: Decoder) throws {
self.init(rawValue: try decoder.singleValueContainer().decode(RawValue.self))
}
}
enum Enum: String, Codable {
case aValidCase, anotherValidCase, unknown
init(from decoder: Decoder) throws {
@alfavata
alfavata / Codable.swift
Last active August 5, 2020 13:13
Codable doesn't play nicely with subclasses
import Foundation
class C: Codable {
var p: String?
}
class D: C {
var q: String?
}