Skip to content

Instantly share code, notes, and snippets.

View eonist's full-sized avatar
🎯
Focusing

André J eonist

🎯
Focusing
View GitHub Profile
@eonist
eonist / CVDisplayLinkOutputCallback.swift
Created March 10, 2016 19:23 — forked from eyeplum/CVDisplayLinkOutputCallback.swift
CVDisplayLinkOutputCallback in Swift
private func createDisplayLink() {
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink)
guard let displayLink = displayLink else {
return
}
let callback: CVDisplayLinkOutputCallback = { (_, _, _, _, _, userInfo) -> CVReturn in
let myView = Unmanaged<MyView>.fromOpaque(COpaquePointer(userInfo)).takeUnretainedValue()
dispatch_async(dispatch_get_main_queue()) {
myView.update()
@eonist
eonist / Keychain.swift
Created March 19, 2016 12:03 — forked from jackreichert/Keychain.swift
Swift Keychain class ( supported Xcode Version 7.0 beta 4 (7A165t) )
import UIKit
import Security
class Keychain {
class func save(key: String, data: NSData) -> Bool {
let query = [
kSecClass as String : kSecClassGenericPassword as String,
kSecAttrAccount as String : key,
kSecValueData as String : data ]
@eonist
eonist / base64.swift
Created March 19, 2016 18:15 — forked from ericdke/base64.swift
Base64 for Swift
let plainString = "foo"
// Encoding
guard let plainData = (plainString as NSString).dataUsingEncoding(NSUTF8StringEncoding) else {
fatalError()
}
let base64String = plainData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
print(base64String) // Zm9v
@eonist
eonist / NSBezierPath+CGPath.swift
Created August 5, 2016 18:11 — forked from lukaskubanek/NSBezierPath+CGPath.swift
NSBezierPath+CGPath.swift
import AppKit
public extension NSBezierPath {
public convenience init(path: CGPath) {
self.init()
let pathPtr = UnsafeMutablePointer<NSBezierPath>.alloc(1)
pathPtr.initialize(self)
@eonist
eonist / methodNames.swift
Created August 25, 2016 10:10 — forked from kristopherjohnson/methodNames.swift
Get method names for an Objective-C class in Swift
import Foundation
/// Given pointer to first element of a C array, invoke a function for each element
func enumerateCArray<T>(array: UnsafePointer<T>, count: UInt32, f: (UInt32, T) -> ()) {
var ptr = array
for i in 0..<count {
f(i, ptr.memory)
ptr = ptr.successor()
}
}
@eonist
eonist / Range+Extensions.swift
Created January 12, 2017 23:57 — forked from emckee4/Range+Extensions.swift
Prospective Range+Extensions for swift 3 intensityAttributingKit
//
// Range+Extensions.swift
// IntensityAttributingKit
//
// Created by Evan Mckee on 3/23/16.
// Copyright © 2016 McKeeMaKer. All rights reserved.
//
import Foundation
@eonist
eonist / touch.swift
Last active September 5, 2017 22:25 — forked from erica/touch.swift
Swift 3 update (playground oriented programming)
import Cocoa
import XCPlayground
import PlaygroundSupport
class TouchView: NSView {
var (path, currentPath) = (NSBezierPath(), NSBezierPath())
override func draw(_ dirtyRect: NSRect) {
guard let contextPtr = NSGraphicsContext.current()?.graphicsPort else {return}
let context = unsafeBitCast(contextPtr, to: CGContext.self)
@eonist
eonist / playground.swift
Last active January 18, 2017 13:09 — forked from h2ero/playground.swift
InteractivePlayground with button that prints hello (swift 3.0.1)
import AppKit
import XCPlayground
import Cocoa
import PlaygroundSupport
extension NSLayoutConstraint {
convenience init(view item: NSView, to toItem: NSView, attribute: NSLayoutAttribute, constant c: CGFloat = 0) {
self.init(item: item, attribute: attribute,
relatedBy: .equal,
toItem: toItem, attribute: attribute,
@eonist
eonist / gist:a21a491496409ed3e203d3f47fdbe35c
Last active February 25, 2017 11:25 — forked from mickmaccallum/gist:c2b322e059c9b3379245
Swift recursive flatmap (Alternative version)
protocol AnyArray{}/*<--Neat trick to assert if a value is an Array, use-full in reflection and when the value is Any but really an array*/
extension Array:AnyArray{}//Maybe rename to ArrayType
func recFlatMap<T>(_ arr:[AnyObject]) -> [T]{
var result:[T] = []
Swift.print("arr.count: " + "\(arr.count)")
arr.forEach{
if($0 is AnyArray){
let a:[AnyObject] = $0 as! [AnyObject]
result += recFlatMap(a)
}else{
@eonist
eonist / URLRequest+cURLCommand.swift
Created March 2, 2017 21:19 — forked from ollieatkinson/URLRequest+cURLCommand.swift
Generate cURL command's from URLRequest's
extension URLRequest {
/// The cURL representation of the URLRequest, useful for debugging and executing requests outside of the app.
var cURLCommand: String {
var command = "curl"
if let httpMethod = httpMethod {
command.append(commandLineArgument: "-X \(httpMethod)")
}