Skip to content

Instantly share code, notes, and snippets.

@bobgodwinx
bobgodwinx / PATs08.swift
Last active June 12, 2018 16:15
PATs08
/// Default `extension` to conform to `TableRow`
extension TableRow {
/// TableRow - conformation
func configure(with model: Any) {
/// Just throw a fatalError
/// because we don't need it.
fatalError()
}
}
//MARK: - `Shadowed` Protocol Based Type Erasure
/// `shadow` protocol
protocol TableRow {
/// - Recieves a parameter of Concrete Type `Any`
func configure(with model: Any)
}
/// `Row` To be shadowed.
protocol Row: TableRow {
associatedtype Model
/// Heterogeneous Requirement and Dynamic dispatch availability
/// Generic Wrapper `AnyCellRow` to match Heterogeneous Types + Dynamic Dispatch
struct AnyCellRow: Row {
private let configureClosure: (Any) -> Void
init<T: Row>(_ row: T) {
configureClosure = { object in
/// Asserting that `object` received is `type` of `T.Model`
guard let model = object as? T.Model else { return }
/// Rows `Interface`
protocol Row {
/// PAT Placeholder for unknown Concrete Type `Model`
associatedtype Model
/// Recieves a parameter of Concrete Type `Model`
func configure(with model: Model)
}
/// Concrete Type `Product`
struct Product { }
/// Concrete Type `Item`
class Cell: TableViewCell {
/// `associatedtype` adoption
typealias T = Detail
/// now the compiler knows and reuires
/// to inject only `Detail` into the `func`
func configure(_ model: T) {
/// Configure your cool cell :)
}
}
/// usage
let extendend = ExtendedDetail()
let detail = Detail()
let detailCell = Cell()
/// This will error
detailCell.configure(extendend)
/// This will be successful.
detailCell.configure(detail)
class Detail {
/// `Concrete Type` that will replace the `T`
}
class ExtendedDetail {
/// Potential `Concrete Type` that will replace the `T`
}
class Cell: TableViewCell {
/// `associatedtype` adoption
protocol TableViewCell {
///Unknown `Concrete Type` declared as `T`
associatedtype T
/// A function that accepts the unknown `Concrete Type`
/// as it's parameter
func configure(_ model: T)
}
@bobgodwinx
bobgodwinx / EncryptAESWithPHP
Last active April 30, 2018 15:51 — forked from krzyzanowskim/EncryptAESWithPHP
How to encrypt data with padding usign AES cipher, complatible with CryptoSwift defaults.
// Marcin Krzyżanwski
// Code for https://github.com/krzyzanowskim/CryptoSwift/issues/20
// PHP
///Please refer to issue: https://github.com/krzyzanowskim/CryptoSwift/issues/20
function encrypt($plaintext, $key, $iv) {
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_CBC, $iv);
return base64_encode($ciphertext);
}
@bobgodwinx
bobgodwinx / swift-compatible-php-aes-encryption-using-mcrypt.php
Created April 30, 2018 15:48 — forked from tlarevo/swift-compatible-php-aes-encryption-using-mcrypt.php
Swift compatible AES Encryption and Decryption with PHP and mcrypt
<?php
// Credit should be given to;
// https://gist.github.com/bradbernard/7789c2dd8d17c2d8304b#file-php-encyrption
// https://gist.github.com/krzyzanowskim/043be69ab3ba9fd5ba58#file-encryptaeswithphp
$data = '{"verification_code":"123456"}';
$key = '1234567890123456';
function aesEncrypt($data, $key) {
$data = addPadding($data);