This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct Weed { | |
public let birthDate: Date | |
private let calendar = Calendar.current | |
private let yearLength = 420 | |
private var today: Date { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import FileKit | |
public typealias RestorablePersistable = Restorable & Persistable | |
public protocol Restorable: Readable {} | |
public protocol Persistable: ReadableWritable { | |
static var fileExtension: String { get } | |
var fileName: String { get } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Restorable { | |
public static func restore(from path: Path) throws -> Self { | |
return try Self.read(from: path) | |
} | |
} | |
extension Persistable { | |
@discardableResult |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public typealias RestorablePersistable = Restorable & Persistable | |
public protocol Restorable: Readable {} | |
public protocol Persistable: ReadableWritable { | |
static var fileExtension: String { get } | |
var fileName: String { get } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface AppDelegate : UIResponder <UIApplicationDelegate> | |
@property (strong, nonatomic) UIWindow *window; | |
@end | |
NewerOlder