Skip to content

Instantly share code, notes, and snippets.

View IgorMuzyka's full-sized avatar

Igor Muzyka IgorMuzyka

View GitHub Profile
import Foundation
struct Weed {
public let birthDate: Date
private let calendar = Calendar.current
private let yearLength = 420
private var today: Date {
@IgorMuzyka
IgorMuzyka / RestorablePersistable.swift
Created May 9, 2018 22:04
Missing extra conveniences of FileKit
import FileKit
public typealias RestorablePersistable = Restorable & Persistable
public protocol Restorable: Readable {}
public protocol Persistable: ReadableWritable {
static var fileExtension: String { get }
var fileName: String { get }
let dave = Person(id: "0", name: "Dave", age: 25)
let file = try! dave.persist(to: .userDocuments) // save dave to user documents
let sameOldDave = try! file.restore() // load dave
let andAgainDave = try! Person.restore(from: file.path) // load dave from path
struct Person: Codable { // your type conforming to Codable
let id: String
let name: String
let age: Int
}
extension Person: RestorablePersistable { // confroming your type to get those convenient methods
static let fileExtension: String = ".person"
var fileName: String { return id }
}
extension File where DataType: Restorable & Codable {
public func restore() throws -> DataType {
return try DataType.restore(from: path)
}
}
extension Readable where Self: Codable & Restorable {
public static func read(from path: Path) throws -> Self {
let data = try Data.read(from: path)
return try FileKit.jsonDecoder.decode(Self.self, from: data)
extension Restorable {
public static func restore(from path: Path) throws -> Self {
return try Self.read(from: path)
}
}
extension Persistable {
@discardableResult
public typealias RestorablePersistable = Restorable & Persistable
public protocol Restorable: Readable {}
public protocol Persistable: ReadableWritable {
static var fileExtension: String { get }
var fileName: String { get }
}
@IgorMuzyka
IgorMuzyka / LoopedArray
Created March 1, 2018 14:19
Looped Array Swift 4
public struct LoopedArray<Element>: Sequence, IteratorProtocol {
private let array: [Element]
private var nextIndex: Int = 0
public init(arrayLiteral elements: Element...) {
array = Array(elements)
}
public init<S: Sequence>(_ s: S) where S.Iterator.Element == Element {
@IgorMuzyka
IgorMuzyka / gist:61a22339d1aff384ac0f
Created May 18, 2015 12:10
Find first subview of class in the view hierarchy
- (UIView *)getFirstViewOfClass:(__unsafe_unretained Class)class inView:(UIView *)view {
UIView *searchedView;
NSArray *subviews = [NSArray new];
for (UIView *subview in view.subviews) {
subviews = [subviews arrayByAddingObject:subview];
if ([subview isMemberOfClass:class]) {
searchedView = subview;
break;
@IgorMuzyka
IgorMuzyka / AppDelegate.h
Created December 17, 2014 19:47
c methods to get class properties classes from properties attributes
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end