Skip to content

Instantly share code, notes, and snippets.

View ffried's full-sized avatar

Florian Friedrich ffried

View GitHub Profile
@ffried
ffried / redundantConstraints.swift
Created May 25, 2021 20:34
Incorrect redundant conformance constraint warnings
protocol P0 {
associatedtype Parent: P0
}
protocol P1: P0 {}
protocol P2: P1 {
associatedtype X: P4 where X.Parent == Self
}
@ffried
ffried / Fibonacci.swift
Last active December 14, 2017 17:05
Fibonacci.swift
public struct Fibonacci<Number: FixedWidthInteger>: Sequence {
public let startsAtZero: Bool
public init(startAtZero: Bool = true) {
startsAtZero = startAtZero
}
public func makeIterator() -> Iterator {
return .first(startsAtZero: startsAtZero)
}
@ffried
ffried / Lazy.swift
Created July 8, 2017 18:10
Sample implementation of a lazy value wrapper
public struct Lazy<T> {
private let initialization: () -> T
public private(set) lazy var: T = self.initialization()
public init(initialization: () -> T) {
self.initialization = initialization
}
public init(other: Lazy<T>) {
import UIKit
open class LayerView<Layer: CALayer>: UIView {
public final override class var layerClass: Swift.AnyClass {
return Layer.self
}
public final var concreteLayer: Layer {
return layer as! Layer
}
@ffried
ffried / NSOptions.swift
Last active July 27, 2016 15:14
NS_OPTIONS-Swift
//ObjC
typedef NS_OPTIONS(NSUInteger, SomeOptions) {
SomeOptionsStarted,
SomeOptionsWorking,
SomeOptionsFailed,
SomeOptionsFinished
};
// Swift
// Generated:
Verifying that +ffried is my blockchain ID. https://onename.com/ffried
- (UIImage *)fixRotation
{
if (self.imageOrientation == UIImageOrientationUp) return self;
CGAffineTransform transform = CGAffineTransformIdentity;
switch (self.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
extension UIImage {
public func imageRotatedByDegrees(degrees: CGFloat) -> UIImage {
let radiansToDegrees: (CGFloat) -> CGFloat = {
return $0 * (180.0 / CGFloat(M_PI))
}
let degreesToRadians: (CGFloat) -> CGFloat = {
return $0 / (180.0 * CGFloat(M_PI))
}
// calculate the size of the rotated view's containing box for our drawing space
import UIKit
import Security
class Keychain {
class func save(key: String, data: NSData) -> Bool {
let query: [String: AnyObject] = [
kSecClass : kSecClassGenericPassword,
kSecAttrAccount : key,
kSecValueData : data ]
@ffried
ffried / UIColor+FFColorWithHex.h
Last active August 29, 2015 14:03
UIColor+FFColorWithHex
//
// UIColor+FFColorWithHex.h
//
// Created by Florian Friedrich on 11.07.14.
// Copyright (c) 2014 Florian Friedrich. All rights reserved.
//
// 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:
//