Skip to content

Instantly share code, notes, and snippets.

View eonist's full-sized avatar
🎯
Focusing

André J eonist

🎯
Focusing
View GitHub Profile
@raphaelhanneken
raphaelhanneken / NSImageExtensions.swift
Last active March 6, 2025 23:01
NSImage extensions for easy resizing, cropping and saving png images.
//
// NSImageExtensions.swift
//
import Cocoa
extension NSImage {
/// The height of the image.
var height: CGFloat {
import QuartzCore
private func demunge(@noescape fn: CGPath.Element -> Void)(ptr: UnsafePointer<CGPathElement>) {
let points = ptr.memory.points
switch ptr.memory.type {
case kCGPathElementMoveToPoint:
fn(.Move(to: points[0]))
case kCGPathElementAddLineToPoint:
fn(.Line(to: points[0]))
case kCGPathElementAddQuadCurveToPoint:
@ericdke
ericdke / resizeNSImage.swift
Last active March 3, 2023 13:47
Resize NSImage
func resize(image: NSImage, w: Int, h: Int) -> NSImage {
var destSize = NSMakeSize(CGFloat(w), CGFloat(h))
var newImage = NSImage(size: destSize)
newImage.lockFocus()
image.drawInRect(NSMakeRect(0, 0, destSize.width, destSize.height), fromRect: NSMakeRect(0, 0, image.size.width, image.size.height), operation: NSCompositingOperation.CompositeSourceOver, fraction: CGFloat(1))
newImage.unlockFocus()
newImage.size = destSize
return NSImage(data: newImage.TIFFRepresentation!)!
}
@JadenGeller
JadenGeller / Exception Handling.swift
Last active November 7, 2015 14:53
Try/Catch Exception Handling in Swift
// A very basic implementation of try/catch blocks in Swift
// Note that this is not thread safe
// Also note that this does not support nested try/catch statements
// We will start with an example
func test(x: Bool) {
try{
println("HI")
if x { throw() }
@kristopherjohnson
kristopherjohnson / methodNames.swift
Last active October 4, 2024 03:51
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()
}
}
@braitom
braitom / .gitignore
Created January 26, 2015 05:36
gitignore for Carthage
# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
@chsh
chsh / run_bg.swift
Created December 31, 2014 08:34
Run NSTask in background for Swift
var taskQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)
dispatch_async(taskQueue, { () -> Void in
let task = NSTask()
task.launchPath = "/bin/ls"
task.arguments = []
let pipe = NSPipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
var dataString = NSString(data: data, encoding: NSUTF8StringEncoding)
@Kapeli
Kapeli / NSCrapField.m
Last active March 9, 2020 18:57
Yosemite NSSearchField Fixes
// 1. Disable centered look & animation:
// 1.1. Subclass NSSearchFieldCell and add:
- (BOOL)isCenteredLook
{
return NO;
}
// 1.2. Subclass NSSearchField and add:
@s-aska
s-aska / Keychain.swift
Last active September 16, 2022 03:37
Swift Keychain class ( supported Xcode 6.0.1 )
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 ]
@jashkenas
jashkenas / semantic-pedantic.md
Last active June 19, 2025 18:41
Why Semantic Versioning Isn't

Spurred by recent events (https://news.ycombinator.com/item?id=8244700), this is a quick set of jotted-down thoughts about the state of "Semantic" Versioning, and why we should be fighting the good fight against it.

For a long time in the history of software, version numbers indicated the relative progress and change in a given piece of software. A major release (1.x.x) was major, a minor release (x.1.x) was minor, and a patch release was just a small patch. You could evaluate a given piece of software by name + version, and get a feeling for how far away version 2.0.1 was from version 2.8.0.

But Semantic Versioning (henceforth, SemVer), as specified at http://semver.org/, changes this to prioritize a mechanistic understanding of a codebase over a human one. Any "breaking" change to the software must be accompanied with a new major version number. It's alright for robots, but bad for us.

SemVer tries to compress a huge amount of information — the nature of the change, the percentage of users that wil