Skip to content

Instantly share code, notes, and snippets.

@mackuba
mackuba / wwdc15.md
Last active August 6, 2022 17:28
New stuff from WWDC 2015

Here's my own list of the interesting stuff announced during this year's WWDC, collected from the keynotes, various Apple docs, blog posts and tweets.

If you're planning to watch the videos, I really recommend this Mac app that helps you download and watch them: https://github.com/insidegui/WWDC.

OS X El Capitan

http://www.apple.com/osx/elcapitan-preview/

  • split view - two apps side by side on full screen
@brocoo
brocoo / WebView.swift
Last active August 29, 2015 14:23
Swift 1.2 function to flush shared cookies
public func flushCookies() {
let storage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
if let cookieJar = storage.cookies as? [NSHTTPCookie] {
for cookie in cookieJar { storage.deleteCookie(cookie) }
}
NSUserDefaults.standardUserDefaults().synchronize()
}
@brocoo
brocoo / Delay.swift
Last active September 27, 2019 13:49
Delay the execution of a given closure on the main queue
public func delay(time: Double, completion: () -> Void) {
let ms = dispatch_time(DISPATCH_TIME_NOW, Int64(time * Double(NSEC_PER_SEC)))
dispatch_after(ms, dispatch_get_main_queue()) {
() -> Void in
completion()
}
}
public func delay(time: Double) -> (()->Void) -> Void {
return {
@brocoo
brocoo / Extensions.swift
Last active April 12, 2021 13:27
Swift UIImage extension for base64 conversion
// MARK: - UIImage (Base64 Encoding)
public enum ImageFormat {
case PNG
case JPEG(CGFloat)
}
extension UIImage {
public func base64(format: ImageFormat) -> String {
class ArrayImpl<T> {
var space: Int
var count: Int
var ptr: UnsafeMutablePointer<T>
init(count: Int = 0, ptr: UnsafeMutablePointer<T> = nil) {
self.count = count
self.space = count
@klundberg
klundberg / weakify.swift
Last active May 13, 2020 08:22
Weakify functions to help you weakly bind instances to static method references
// these functions take a swift class's statically referenced method and the instance those methods
// should apply to, and returns a function that weakly captures the instance so that you don't have
// to worry about memory retain cycles if you want to directly use an instance method as a handler
// for some object, like NSNotificationCenter.
//
// For more information, see this post:
// http://www.klundberg.com/blog/capturing-objects-weakly-in-instance-method-references-in-swift/
func weakify <T: AnyObject, U>(owner: T, f: T->U->()) -> U -> () {
return { [weak owner] obj in
@brocoo
brocoo / Swift NSError extension
Created March 30, 2015 11:19
Extension of NSError that allow custom personalised errors for a project (Swift 1.2)
// MARK: - NSError
public enum ErrorType: Int {
case Unknown = 1
case NotAuthenticated = 2
func localizedUserInfo() -> [String: String] {
var localizedDescription: String = ""
var localizedFailureReasonError: String = ""
@brocoo
brocoo / Result.swift
Last active August 29, 2015 14:16
A result enum, storing a boxed type or a NSError meant to be return from any process (API call, etc) (Swift 1.2)
// MARK: - Box
final public class Box<T> {
public let unbox:T
public init(_ value: T) { self.unbox = value }
}
// MARK: - Result enum
public enum Result<T: Printable> {
@brocoo
brocoo / Swift NSNotification extension
Last active May 13, 2016 09:51
Extension of NSNotification to use a key enum as the notification name.
public enum NotificationKey: String {
case UserSignedIn = "UserSignedInNotification"
case UserSignedOut = "UserSignedOutNotification"
case SomeOtherEvent = "SomeOtherEventNotification"
}
extension NSNotificationCenter {
func addObserver(observer: AnyObject, selector aSelector: Selector, key aKey: NotificationKey) {
self.addObserver(observer, selector: aSelector, name: aKey.rawValue, object: nil)
@chriseidhof
chriseidhof / routes.swift
Created August 17, 2014 21:04
Type-safe routes in Swift
//
// main.swift
// Routes
//
// Created by Chris Eidhof on 17/08/14.
// Copyright (c) 2014 Chris Eidhof. All rights reserved.
//
import Foundation