Skip to content

Instantly share code, notes, and snippets.

protocol JSONValueConvertible {
var jsonValue: JSONValue { get }
}
protocol JSONValue: JSONValueConvertible { }
extension JSONValue {
var jsonValue: JSONValue {
return self
extension BidirectionalCollection {
func makeBidirectionalIterator() -> BidirectionalIterator<Self, Index> {
return BidirectionalIterator<Self, Index>(collection: self)
}
}
let a = [1, 2, 3, 4]
struct APIError: NetworkError {
let json: JSON
let apiCode: Int?
let message: String?
let httpResponse: HTTPURLResponse
```struct TreeNode<Element> {
var element: Element
var children: [TreeNode<Element>]
}
class TreeNodeIterator<Element>: IteratorProtocol {
var node: TreeNode<Element>
var index = -1
var currentIterator: TreeNodeIterator<Element>?
import UIKit
protocol StoryboardInitializable {
static var storyboardName: String { get }
static var storyboardIdentifier: String { get }
static func makeFromStoryboard() -> Self
}
extension StoryboardInitializable {
@khanlou
khanlou / ThisCompiles.swift
Created May 4, 2017 15:06
Protocols with associatedtypes
import Foundation
protocol JSONInitializable {
init?(data: Data)
}
protocol Request {
associatedtype OutputType: JSONInitializable
extension Sequence {
func uniqueElements(by elementsEqual: (Element, Element) -> Bool) -> [Element] {
var result: [Element] = []
for element in self {
if !result.contains(where: { resultElement in elementsEqual(element, resultElement) }) {
result.append(element)
}
}
return result
}
import Foundation
import HTTP
import Vapor
public protocol SideEffect {
func perform() throws
}
public protocol Command: ResponseRepresentable {
public struct NilError: Error, CustomStringConvertible {
let file: String
let line: Int
public init(file: String = #file, line: Int = #line) {
self.file = file
self.line = line
}
#import <Foundation/Foundation.h>
@interface Unit : NSObject
@end
typedef void (^SuccessBlock)(id _Nonnull object);
typedef id _Nullable (^ _Nonnull ThenBlock)(id _Nonnull object);
typedef void (^FailureBlock)(NSError * _Nonnull error);