Skip to content

Instantly share code, notes, and snippets.

// BASIC SWITCH
enum Media { case Book }
extension Media : CustomStringConvertible
{
var description: String {
switch self {
case .Book: return "bible"
}
import UIKit
import PluggableApplicationDelegate
@UIApplicationMain
class AppDelegate: PluggableApplicationDelegate
{
override var services: [ApplicationService] {
return [
LoggerApplicationService()
]
@janodev
janodev / AutoLayout.swift
Created April 30, 2019 14:05
Buttons with equal width using Visual Format Language
import UIKit
public extension UIView {
var autoLayout: AutoLayout {
return AutoLayout(self)
}
}
@janodev
janodev / gist:5341598
Created April 8, 2013 23:44
Text to speech on iOS with private framework.
// Not App Store safe. Only available in real devices.
#define RTLD_LAZY 0x1
#define RTLD_NOW 0x2
#define RTLD_LOCAL 0x4
#define RTLD_GLOBAL 0x8
NSObject *voiceSynthesizer;
void *voiceServices;
@janodev
janodev / karabiner.json
Created December 10, 2018 01:16
My Karabiner rules containing "WASD arrow Keys toggled by Capslock". See https://stackoverflow.com/a/53698322/412916
{
"global": {
"check_for_updates_on_startup": true,
"show_in_menu_bar": true,
"show_profile_name_in_menu_bar": false
},
"profiles": [
{
"complex_modifications": {
"parameters": {
@janodev
janodev / Person.m
Created June 27, 2013 11:48
Dynamic get/set. Watch it, I'm using [[NSThread currentThread] threadDictionary] for thread-safe access.
#import <objc/runtime.h>
#import <Foundation/Foundation.h>
@interface Person : NSObject
@end
@implementation Person
@end
@interface Person(dynamicProperties)
@janodev
janodev / idioms.swift
Last active June 29, 2018 16:17
initialising idioms
import UIKit
// this one doesn’t require a helper function
var label: UILabel = {
$0.backgroundColor = .blue
$0.text = "This is a playground"
$0.textColor = .white
$0.textAlignment = .center
return $0
@janodev
janodev / ObjC.h
Last active April 16, 2018 09:18
Capture ObjC exceptions. From https://stackoverflow.com/a/36454808/412916
#import <Foundation/Foundation.h>
@interface ObjC: NSObject
+ (BOOL)catchException:(void(^)(void))tryBlock error:(__autoreleasing NSError **)error;
@end
@janodev
janodev / ControllerLifecycle.swift
Created April 16, 2018 09:06
Unit testing a view controller
// From https://albertodebortoli.com/2018/03/12/easy-view-controller-unit-testing/
import XCTest
import UIKit
class ControllerLifecycle<T: UIViewController>
{
private lazy var this = type(of: self).self
private var rootWindow: UIWindow?
var rootController: T? {
@janodev
janodev / Downloader.swift
Created April 8, 2018 13:51
Weakly retaining an object in a closure without having to write [weak]
// I saw this trick in “Do you often forget [weak self], here is a solution”
// https://medium.com/anysuggestion/preventing-memory-leaks-with-swift-compile-time-safety-49b845df4dc6
import UIKit
class ViewController: UIViewController {
// move this variable inside viewDidLoad to see it being released
let downloader = Downloader()