Skip to content

Instantly share code, notes, and snippets.

View groue's full-sized avatar

Gwendal Roué groue

View GitHub Profile
#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: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: () -> ()
}
@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: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) {

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 {
// 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
// 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 = 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
/// Given two sorted sequences (left and right), this function emits "merge steps"
/// which tell whether elements are only found on the left, on the right, or on
/// both sides.
///
/// Both sequences do not have to share the same element type. Yet elements must
/// share a common comparable *key*.
///
/// Both sequences must be sorted by this key.
///
/// Keys must be unique in both sequences.
@groue
groue / synchronize.swift
Last active March 9, 2021 16:02
How to synchronize an SQLite table with a JSON payload using https://github.com/groue/GRDB.swift
// This sample code shows how to use GRDB to synchronize a database table
// with a JSON payload. We use as few SQL queries as possible:
//
// - Only one SELECT query.
// - One query per insert, delete, and update.
// - Useless UPDATE statements are avoided.
import Foundation
import GRDB