Skip to content

Instantly share code, notes, and snippets.

View ralfebert's full-sized avatar

Ralf Ebert ralfebert

View GitHub Profile
@ralfebert
ralfebert / swift-kvo-example.swift
Last active October 18, 2020 21:41 — forked from correia/swift-kvo-example.swift
Swift 1.1: dynamic for observable properties neccessary, observeValueForKeyPath signature changed
//
// Swift-KVO
//
// Created by Jim Correia on 6/5/14.
// Copyright (c) Jim Correia. All rights reserved.
//
// Update: 6/17/2014
//
// KVOContext has gone away; use the same idiom you'd use from Objective-C for the context
@ralfebert
ralfebert / create
Last active December 11, 2020 13:13
Create and open a file from the shell using simple templates
#!/usr/bin/env ruby
# Create and open a file from the shell using simple templates
# https://gist.github.com/ralfebert/cdc30dbc9d2c2356d8f9b6b7c1492888/edit
require 'date'
require 'shellwords'
require 'ostruct'
require 'optionparser'
@ralfebert
ralfebert / slice.py
Last active April 12, 2021 08:29
Export images from Sketch to xcasset (by @ryangomba)
#!/usr/bin/env python
import os
import sys
import subprocess
import shutil
import json
import re
import time
from distutils.spawn import find_executable
import SwiftUI
class DemoModel : ObservableObject {
enum State {
case value(Date)
}
@Published var counter = 0
@Published var state = State.value(Date())
extension Comparable {
mutating func clamp(_ limits: ClosedRange<Self>) {
self = min(max(self, limits.lowerBound), limits.upperBound)
}
mutating func clamp(_ limits: PartialRangeFrom<Self>) {
self = max(self, limits.lowerBound)
}
mutating func clamp(_ limits: PartialRangeThrough<Self>) {
self = min(self, limits.upperBound)
}
// based on https://sarunw.com/posts/how-to-sort-by-multiple-properties-in-swift/
struct BlogPost {
let title: String
let pageView: Int
let sessionDuration: Double
}
extension BlogPost {
static var examples: [BlogPost] = [
struct Format {
/// 12.03.2021
static let mediumDate: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
return formatter
}()
/// 12.03.2021 12:34
import CoreHaptics
import SwiftUI
enum FeedbackType {
case selection, success, error
}
class Haptics {
@AppStorage("HaptikFeedbackEnabled") var haptikFeedbackEnabled = true

FB9165511: Swift compiler accepts an invalid circular reference in some conditions

The following code example doesn't compile because of the circular reference. Except if you uncomment the line that uses the weak self. It's still an invalid circular reference, but all of a sudden the compiler accepts it. Happens with Xcode 12.5 and 13 Beta 1.

class Flupp {
    init() {
        print("Flupp!")
    }
 deinit {
@ralfebert
ralfebert / .bashrc
Created August 9, 2010 19:16
git settings
# Prompt (Debian)
source /usr/local/bin/git-completion.sh
# Prompt (OS X + homebrew)
source /usr/local/etc/bash_completion.d/git-completion.bash
PS1="\[\033[31;38m\]\w\[\033[1;31m\]\$(__git_ps1)\[\033[00m\] "
export GIT_PS1_SHOWDIRTYSTATE=1