Skip to content

Instantly share code, notes, and snippets.

Avatar

Tobias Due Munk duemunk

View GitHub Profile
View Animating AnyView vs Group
import SwiftUI
// Animates nicely since the type of both returns are the same, i.e. AnyView<Text>
func gimme(toggle: Bool) -> AnyView {
if toggle {
return AnyView(
Text("Hello World")
)
} else {
return AnyView(
@duemunk
duemunk / PortraitEffectsMatteCoreImage.swift
Created Jun 14, 2018
Open Portrait Effects Matte in iOS 12
View PortraitEffectsMatteCoreImage.swift
let data = try Data(contentsOf: fileUrl)
let portraitMatteImage = CIImage(data: data, options: [.auxiliaryPortraitEffectsMatte: true])
View HalfFloat.swift
/*
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
View Map over KeyPaths in Swift 4.0.swift
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]
View CustomViewLayoutWithAutoLayoutSupport.swift
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 Feb 15, 2017
An example of how you can use the error handling in Swift 2.0 for an asynchronous API.
View AsynchronousThrow.swift
typealias EmptyResult = () throws -> ()
typealias AsyncResult = (EmptyResult) -> ()
typealias JsonResult = () throws -> JSON
typealias AsyncJsonResult = (JsonResult) -> ()
class GetResponse {
func main() {
View Main queue?.swift
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
Auto Layout in Swift
View AutoLayout.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 / No storyboard
Last active Aug 29, 2015
iOS Application without storyboard
View No 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 {
@duemunk
duemunk / Guilty
Last active Jun 23, 2016
Operator overload badge
View Guilty
<img src="http://img.shields.io/badge/Operator_overload-guilty-red.svg" height="20" alt="Uses operator overloads"/>