Skip to content

Instantly share code, notes, and snippets.

@berikv
berikv / log.swift
Last active August 29, 2015 14:13
Simple logging in swift
func log<T>(message: T, fileName: String = __FILE__, lineNumber: Int = __LINE__, args: CVarArgType...) {
let date = NSDate()
let processInfo = NSProcessInfo()
let appName = processInfo.processName
let combined = String(format: String(stringInterpolationSegment: message), arguments: args)
println("\(date) \(appName) [\(fileName.lastPathComponent):\(lineNumber)] \(combined)")
}
@berikv
berikv / example.txt
Created February 20, 2015 10:55
LLDB helper for printing the position of a view inside it's window
(lldb) posinwindow 0x796a0e60
(CGPoint) $58 = (x=711, y=205.5)
@berikv
berikv / dispatch_after_swift.codesnippet
Last active August 29, 2015 14:17
Xcode snippet for dispatch_after in Swift. Move this file to ~/Library/Developer/Xcode/UserData/CodeSnippets. Use Finder CMD+Shift+G
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDECodeSnippetCompletionPrefix</key>
<string>dispatch_after</string>
<key>IDECodeSnippetCompletionScopes</key>
<array>
<string>CodeExpression</string>
</array>
@berikv
berikv / ExponentialMovingAverage.swift
Last active December 30, 2022 18:25
Exponential Moving Average
func exponentialMovingAverage(currentAverage: Double, newValue: Double, smoothing: Double) -> Double {
return smoothing * newValue + (1 - smoothing) * currentAverage
}
// Usage:
// var a = 3
// a = exponentialMovingAverage(a, 8, 0.5)
// Swift 2
@berikv
berikv / gist:f17b6e18a8de011ec71f
Created March 27, 2015 19:46
Is user a simulator
let simulator = UIDevice.currentDevice().model.rangeOfString("Simulator") != nil
//
// UIColor+hex.swift
//
// Created by Berik Visschers on 05-21.
// Copyright (c) 2015 Berik Visschers. All rights reserved.
//
import UIKit
extension UIColor {
@berikv
berikv / gifify
Last active December 21, 2021 16:51
Transform a movie file to a gif. Specialised for demo's of Mobile app screen recordings.
#!/bin/bash
FFMPEG=$(command -v ffmpeg)
INFILE=$1
OUTFILE="${INFILE}.gif"
TMPFILE="${INFILE}_gifify_palette.png"
if ! $FFMPEG -L > /dev/null 2>&1; then
echo "Run: brew install ffmpeg"
exit 1
@berikv
berikv / StopThread.swift
Last active December 7, 2017 15:05
Stop the current thread for a specified period using a semaphore
let deadlineTime = DispatchTime.now() + .seconds(100)
let semaphore = DispatchSemaphore(value: 1)
DispatchQueue.global().asyncAfter(deadline: deadlineTime) {
semaphore.signal()
}
semaphore.wait()
@berikv
berikv / .gitalias.sh
Last active July 20, 2021 11:27
Some convenient and readable git aliases
# Paste these lines into your ~/.bashrc or other shell initialisation script
# Note that for most of these, your gitconfig has to have the aliasses from the .gitconfig in this gist
alias co='git checkout'
alias st='git status'
alias add='git add'
alias commit='git commit'
# Amend anything that is staged
@berikv
berikv / dev_env
Last active May 1, 2019 20:23
Setup headless raspberry pi from scratch
1) `sudo apt-get install git-core`
Setup git remote
2) `scp -r <your project> <your username>@<ip of raspberry pi>:`
3) `ssh <your username>@<ip of raspberry pi>:`
4) `cd <your project>`
5) `git config --bool core.bare true`
6) Go back to your local machine (Ctrl-D)