Skip to content

Instantly share code, notes, and snippets.

@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 = ""
@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
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
@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 {
@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 / 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()
}
@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 / Settings.swift
Created July 22, 2015 20:24
Settings struct
struct Settings {
// Make sure to add the "-D DEBUG" flag in the project settings for the Swift Compiler
static var Debug: Bool {
#if DEBUG
return true
#else
return false
#endif
}
@brocoo
brocoo / Point.swift
Created July 22, 2015 22:19
CGPoint extension for SpriteKit
extension CGPoint {
public enum CoordinateSystem {
case UIKit
case SpriteKit
}
public func coordinates(from from: CoordinateSystem, to: CoordinateSystem) -> CGPoint {
if from == to { return self }
else {
@brocoo
brocoo / Extensions.swift
Created July 23, 2015 11:34
Perform closure once for a given key
// MARK: - NSUserDefaults
extension NSUserDefaults {
class func performOnceForKey(key: String, perform: () -> Void, elsePerform: (() -> Void)? = nil) {
let once = self.standardUserDefaults().objectForKey(key)
self.standardUserDefaults().setBool(true, forKey: key)
self.standardUserDefaults().synchronize()
if once == nil { perform() }