Skip to content

Instantly share code, notes, and snippets.

@IanKeen
IanKeen / KeyValueStorable.swift
Last active November 1, 2015 18:38
A protocol I like to use in my apps anywhere something like NSUserDefaults or KeyChain is normally used... allows for easy swapping of implementation. Also provided is StringDictionary() which can be used when testing
protocol KeyValueStorable {
subscript (key: String) -> AnyObject? { get set }
mutating func removeAll() -> Void
}
extension NSUserDefaults: KeyValueStorable {
subscript (key: String) -> AnyObject? {
get { return self.objectForKey(key) }
set { self.setObject(newValue, forKey: key) }
@landonf
landonf / 1-README.md
Last active June 2, 2016 06:49
Swift Result Type

A result type (based on swiftz) that can represent either an error or success:

enum Result<X, T> {
  case Err(() -> X)
  case Ok(() -> T)
}

Now we need a way to chain multiple results together without lots of nesting of if statements -- or exceptions. To do so, we can define a new bind (result, next) operator (implementation borrowed from swiftz) that operates on Result types (a.k.a flatMap or >>=):

  • If the result is Err, the result is immediately returned.
@azamsharp
azamsharp / gist:4836d90bc50b7ad52c042a9bb9e92e46
Created January 11, 2017 20:41
View Controller Containment Injecting Empty Views
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
typealias JSONDictionary = [String:Any]
struct Resource<T> {
let url :URL
@glessard
glessard / shuffle.swift
Last active June 29, 2018 02:48
Shuffle a CollectionType
//
// shuffle.swift
//
// Created by Guillaume Lessard on 2014-08-28.
// Copyright (c) 2016 Guillaume Lessard. All rights reserved.
//
// https://github.com/glessard/shuffle
// https://gist.github.com/glessard/7140fe885af3eb874e11
//
@chriseidhof
chriseidhof / json.swift
Last active March 21, 2019 07:45
Reflection
import Cocoa
struct Person {
var name: String = "John"
var age: Int = 50
var dutch: Bool = false
var address: Address? = Address(street: "Market St.")
}
struct Address {
@brimelow
brimelow / ContentView.swift
Created June 13, 2019 07:51
Random 100 Circles Animation in SwiftUI
//
// ContentView.swift
// DrawingGroup
//
// Created by Lee Brimelow on 6/12/19.
// Copyright © 2019 Lee Brimelow. All rights reserved.
//
import SwiftUI
@CodeSlicing
CodeSlicing / ConcentricArcsDemo.swift
Created January 23, 2020 23:29
Demo showing animating concentric arcs on a timer without pulsing with the timer duration
//
// ConcentricArcsDemo.swift
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
# This snippet shows how to enable dyld `closured` and how to fix `read` implementation in dyld using lldb.
# Following text is a bash/lldb command mix and it contains the relevant stdout content and my comments.
// `DYLD_PRINT_WARNINGS=1` environment variable will print dyld logs
$ DYLD_PRINT_WARNINGS=1 lldb /Users/kamil.borzym/tmp/TestMacApp.app/Contents/MacOS/TestMacApp
(lldb) process launch --stop-at-entry
(lldb) image list
[ 0] D355CCB5-8584-33E3-8DF7-7BA95BAD41AF 0x0000000100000000 /Users/kamil.borzym/tmp/TestMacApp.app/Contents/MacOS/TestMacApp
@amittkashyap
amittkashyap / Persons.swift
Last active March 28, 2020 14:02
In this gist I will show you how to use custom keyDecodingStrategy/keyEncodingStrategy in new Codable/Decodable protocols in swift 4.1 with a simple example.
let json = """
[
{
"first-Name": "Taylor",
"last-Name": "swift",
"age": 28
},
{
"first-Name": "Tom",
"last-Name": "hanks",