Skip to content

Instantly share code, notes, and snippets.

@cemaleker
cemaleker / JSONStringRawRepresentable.swift
Created May 25, 2023 11:08
Using SwiftUI AppStorage to cache Codable entities
import Foundation
struct JSONStringRawRepresentable<T: Codable>: RawRepresentable {
typealias RawValue = String
var value: T
init(value: T) {
self.value = value
}
### Keybase proof
I hereby claim:
* I am cemaleker on github.
* I am cemaleker (https://keybase.io/cemaleker) on keybase.
* I have a public key ASDxNd1ZEbA37ckIj89Su08jqHW29FCUY3ZSkMlTm9N0Qwo
To claim this, I am signing this object:
@cemaleker
cemaleker / StringRepresentable.swift
Last active October 16, 2019 19:39
StringRepresentable casting with generics
enum StringRepresentableType: String {
case foo
case bar
}
typealias JSONDictionary = [String: Any]
extension JSONDictionary {
func value<T, R>(for key: String) -> T? where T: RawRepresentable, T.RawValue == R {
@cemaleker
cemaleker / README
Last active March 26, 2019 09:53
gapless-sample-files
Sample gapless test files with required metadata.
@cemaleker
cemaleker / defer.swift
Last active July 30, 2018 20:17
How swift defer captures
var baz: String?
func deferSomething(_ quux: inout String?) {
var foo: String
quux = "uier"
defer { print("__\(foo)__\(baz)__\(quux)__") }
quux = "grault"
foo = "bar"
}
func dontDeferSomething(_ quux: inout String?) {
@cemaleker
cemaleker / Sequence+StableSorted.swift
Created July 9, 2018 08:19
Stable sorting for swift sequences
extension Sequence {
/// return a sorted collection using a stable sort algorithm
///
/// - Parameter areInIncreasingOrder: return nil when two element are equal
/// - Returns: the sorted collection
///
/// Shamelessly copied from https://stackoverflow.com/a/45585365
///
public func stableSorted(by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows -> [Element] {
@cemaleker
cemaleker / fix-redux-default.php
Created December 23, 2017 17:29
Fixes symlink resolution on redux options of embedded redux framework in neighborhood wordpress theme.
<?php
function __rd_dirname($path) {
$path = dirname($path);
return __rd_resolve_real($path);
}
function __rd_resolve_real($path) {
return realpath(is_link($path) ? readlink($path) : $path);
}
@cemaleker
cemaleker / UIViewController.swift
Last active June 20, 2017 10:34
Container view controller helpers
extension UIViewController {
func attachChildViewController(_ viewController: UIViewController) {
attachChildViewController(viewController, to: self.view)
}
func attachChildViewController(_ viewController: UIViewController, to view: UIView) {
self.addChildViewController(viewController)
view.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)