Skip to content

Instantly share code, notes, and snippets.

View sveinhal's full-sized avatar
Writing code

Svein Halvor Halvorsen sveinhal

Writing code
View GitHub Profile
// RandomDecoder.swift
// Utils
//
// Created by Svein Halvor Halvorsen on 24/10/2019.
import Foundation
protocol RandomInstantiable {
static func randomInstance<G: RandomNumberGenerator>(using: inout G) -> Self
}
struct AnyGenerator<Element>: GeneratorType {
init<G: GeneratorType where G.Element == Element>(_ gen: G) {
var gen = gen
self._next = { gen.next() }
}
private let _next: () -> Element?
func next() -> Element? {
return _next()
}
}
@sveinhal
sveinhal / PageControl.swift
Created August 28, 2015 11:11
A page control subclass with customizable dots
import UIKit
@IBDesignable
final class PageControl: UIPageControl {
@IBInspectable var activeImage: UIImage?
@IBInspectable var inactiveImage: UIImage?
private func updateDotImages() {
for (i, subview) in enumerate(subviews) {
@sveinhal
sveinhal / PairSequence.swift
Last active August 29, 2015 14:24
A sequence type that iterates over pairs of the underlying sequence
import Foundation
struct PairGenerator<T: GeneratorType> : GeneratorType {
typealias Pair = (first: T.Element?, second: T.Element?)
var baseGenerator: T
var previousPair: Pair
init(_ baseGenerator: T) {
self.baseGenerator = baseGenerator
self.previousPair = (nil, nil)
}
@sveinhal
sveinhal / Implementation.swift
Created July 2, 2015 10:50
How to implement an Obj-C protocol with custom getter name in Swift
class SomeClass: SomeProtocol {
private var _enabled: Bool = false
var enabled: Bool {
@objc(isEnabled) get { return _enabled }
set { _enabled = newValue }
}
}
import UIKit
class SlideOutTransitioning: NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
private var presenting: Bool = false
private let duration = 0.3
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.presenting = true
return self