Skip to content

Instantly share code, notes, and snippets.

View duemunk's full-sized avatar

Tobias Due Munk duemunk

View GitHub Profile
@duemunk
duemunk / ChainingSyntax.swift
Last active August 29, 2015 14:05
Chaining syntax
struct Foo {
private let privateObject: PrivateObjectType
private init(_ privateObject: PrivateObjectType) {
self.privateObject = privateObject
}
}
extension Foo { // Static methods
@duemunk
duemunk / No storyboard
Last active August 29, 2015 14:05
iOS Application without storyboard
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
if let window = window {
let mainQueueA = dispatch_get_global_queue(qos_class_main(), 0)
let mainQueueB = dispatch_get_main_queue()
// This works fine
dispatch_apply(3, mainQueueA) { i in
println("A: \(i)")
}
println("Yep")
// This never calls the block and hangs the main thread
@duemunk
duemunk / AutoLayout.swift
Last active May 6, 2016 08:45
Auto Layout in Swift
//
// AutoLayout.swift
// Tobias Due Munk
//
// Created by Tobias Due Munk on 26/08/14.
// Copyright (c) 2014 Tobias Due Munk. All rights reserved.
//
// Example usage:
//
// view1.topInset(20, relation: .Equal, priority: 200)
@duemunk
duemunk / Guilty
Last active June 23, 2016 12:11
Operator overload badge
<img src="http://img.shields.io/badge/Operator_overload-guilty-red.svg" height="20" alt="Uses operator overloads"/>
class View: UIView {
override var intrinsicContentSize: CGSize {
// Fallback to super class implementation of intrinsicContentSize
return viewContentSize() ?? super.intrinsicContentSize
}
override init(frame: CGRect) {
super.init(frame: frame)
}
@duemunk
duemunk / AsynchronousThrow.swift
Last active February 15, 2017 02:28
An example of how you can use the error handling in Swift 2.0 for an asynchronous API.
typealias EmptyResult = () throws -> ()
typealias AsyncResult = (EmptyResult) -> ()
typealias JsonResult = () throws -> JSON
typealias AsyncJsonResult = (JsonResult) -> ()
class GetResponse {
func main() {
@duemunk
duemunk / PortraitEffectsMatteCoreImage.swift
Created June 14, 2018 14:39
Open Portrait Effects Matte in iOS 12
let data = try Data(contentsOf: fileUrl)
let portraitMatteImage = CIImage(data: data, options: [.auxiliaryPortraitEffectsMatte: true])
/*
MIT License
Copyright 2018 Tobias Due Munk
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
extension Sequence {
func map<T>(_ keyPath: KeyPath<Element, T>) -> [T] {
return self.map {
$0[keyPath: keyPath]
}
}
func flatMap<T>(_ keyPath: KeyPath<Element, T?>) -> [T] {
return self.flatMap {
$0[keyPath: keyPath]