Skip to content

Instantly share code, notes, and snippets.

View groue's full-sized avatar

Gwendal Roué groue

View GitHub Profile
// Library code
class Model { }
protocol Fetchable {
typealias FetchableType = Self
static func fetchAll(sql: String) -> [FetchableType]
}
extension Fetchable where Self : Model, FetchableType == Self {
static func fetchAll(sql: String) -> [FetchableType] {
return [] // bare minimal, for demonstration purpose
// Library code
class Model { }
protocol Fetchable {
typealias FetchableType
static func fetchAll(sql: String) -> [FetchableType]
}
extension Fetchable where Self : Model, FetchableType == Self {
static func fetchAll(sql: String) -> [FetchableType] {
return [] // bare minimal, for demonstration purpose

This blog post shows how Swift protocols can help using CoreData: http://martiancraft.com/blog/2015/07/objective-c-swift-core-data/

Following this advice, I have tried this technique, with success, but with a request for improvement.

Below you will find a bare minimal view of the API I'm working on. Notice the not nice Person.self in the line db.fetchAll(Person.self, "SELECT * FROM persons"). I want to improve that, and the linked blog post was a great deal of inspiration.

// Library
class RowModel {} // Equivalent of NSManagedObject in Core Data
class Database {
@groue
groue / gist:f2ecc98b8301ed63d843
Created July 12, 2015 14:16
A function declared as rethrows that synchronously executes a throwing block in a dispatch_queue.
import Foundation
// A function declared as rethrows that synchronously executes a throwing
// block in a dispatch_queue.
//
// Based on Ransak's answer to https://forums.developer.apple.com/message/19685
func perform_sync(queue: dispatch_queue_t, block: () throws -> Void) rethrows {
func impl(queue: dispatch_queue_t, block: () throws -> Void, block2:((ErrorType) throws -> ()) ) rethrows {
var blockError: ErrorType? = nil
dispatch_sync(queue) {
@groue
groue / gist:401e88b7148bc126adb3
Last active August 29, 2015 14:15
GRMustache.swift cycle
import Mustache
// Wrap the result in a section, and the key `cycleKey` will enumerate and cycle
// around boxes, one after the other.
func cyclingBox(key cycleKey: String, #boxes: [MustacheBox]) -> MustacheBox {
var index = 0
return Box { (key: String) in
if key == cycleKey {
let box = boxes[index]
index = (index + 1) % boxes.count
@groue
groue / gist:598ffd92103fbcf29b26
Last active March 14, 2024 18:28
Swift recursive walking of JSON-like structure: values (any Swift type, any ObjC class), array of values, dictionaries of values.
import Foundation
// =============================================================================
// Box
// The Box
struct Box {
let value: Any
let walk: () -> ()
}
#import "GRMustache.h"
NSString *templateString = @"{{# repeat(Red) }}<img src=\"img/red.png\">{{/ repeat(Red) }}}";
GRMustacheTemplate *template = [GRMustacheTemplate templateFromString:templateString error:NULL];
id obj = @{
@"Red": @10,
@"repeat": [GRMustacheFilter filterWithBlock:^id(NSNumber *number) {
NSInteger count = [number integerValue];
return [GRMustache renderingObjectWithBlock:^NSString *(GRMustacheTag *tag, GRMustacheContext *context, BOOL *HTMLSafe, NSError *__autoreleasing *error) {
@groue
groue / gist:5610588
Last active December 17, 2015 12:29

Hello,

I'm working on a class that synthesizes its own property accessors when the user subclasses it, and declares properties @dynamic.

That class is part of the GRMustache library, and is documented in https://github.com/groue/GRMustache/blob/master/Guides/view_model.md

Today, the current implementation works, and is robust. The end user has a very simple API - he just has to declare dynamic properties in order to be granted with the features he needs. I'm quite happy of that.


@groue
groue / _documentation.md
Created September 29, 2012 11:05
GRMustache: variadic filters
@groue
groue / Document.mustache
Created September 27, 2012 07:55
GRMustache: use dynamic partials to render a collection of objects
{{movies}} {{! one movie is not enough }}