Skip to content

Instantly share code, notes, and snippets.

View lukaskubanek's full-sized avatar

Lukas Kubanek lukaskubanek

View GitHub Profile
//
// Created by Tony Arnold on 4/03/2014.
// Copyright (c) 2014 The CocoaBots. All rights reserved.
//
#import "NSView+TCBAdditions.h"
#import <objc/runtime.h>
@implementation NSView (TCBAdditions)
@tjw
tjw / protocol-type-member.swift
Last active August 29, 2015 14:08
Can't access members of protocol types
public protocol MeasurementUnit {
// If we have one of this unit, how many millimeters is it?
class var asMillimeters: Double { get }
}
public class Inch : MeasurementUnit {
public class var asMillimeters: Double {
get {
return 25.4
}
}
import Foundation
/*
Lets suppose you have a class called "Action" which has to represent an user action in a drawing app. There are a fixed number of different actions: Delete, Add, Modify. Some of those action types have different properties. For example:
If the user adds something, the add action contains "what" the user has added and the "content" of the new object.
If the user modifies something, the modify action contains "what" has been edited, the "previousContent" and the "newContent".
So there are basically at least three different approaches how you can model that. Which one is the best?
//
// SSScrollView.h
//
/*
Get a delegate callback when the scroll view scrolls.
*/
#import <Cocoa/Cocoa.h>
@aquarius
aquarius / gist:3787222
Created September 26, 2012 10:24
Use NSSharingService from a text only NSToolbar
// In toolbarWillAddItem: create a new NSMenu
// set the delegate and add the menu as part of a NSMenuItem as menuFormRepresentation
- (void)menuNeedsUpdate:(NSMenu *)menu
{
// clear out in case we can no longer provide this service
[menu removeAllItems];
NSURL *fileURL = [self.document fileURL];
@nielsbot
nielsbot / gist:5294660
Last active December 15, 2015 17:09
Make sure your layer is pixel-aligned when setting it's position. (Avoids things looking blurry)
// when setting the position of a layer, use this:
// layer.position = [ layer pixelAlignedPostionForPoint:<originalPoint> ] ;
@implementation CALayer (SetPositionPixelAligned)
-(CGPoint)pixelAlignedPositionForPoint:(CGPoint)p
{
CGSize size = self.bounds.size ;
CGPoint anchorPoint = self.anchorPoint ;
// Defines a yet undocumented method to add a warning if super isn't called.
#ifndef NS_REQUIRES_SUPER
#if __has_attribute(objc_requires_super)
#define NS_REQUIRES_SUPER __attribute((objc_requires_super))
#else
#define NS_REQUIRES_SUPER
#endif
#endif
@joshaber
joshaber / gist:4238342
Created December 8, 2012 02:54
ReactiveCocoa stopwatch with reset
self.startButton.rac_command = [RACCommand command];
self.stopButton.rac_command = [RACCommand command];
self.resetButton.rac_command = [RACCommand command];
static const CGFloat interval = 0.01;
__unsafe_unretained id weakSelf = self;
// Signal id -> Signal Signal Number
// Map each click of the start button to a signal that fires at our interval
// and stops when the stop button's clicked.
id<RACSignal> start = [self.startButton.rac_command map:^(id _) {
@austinzheng
austinzheng / swift-kvc.swift
Last active May 27, 2016 03:44
An exploration of a possible KVC-like API for a future version of Swift.
/// A statically typed view into a get-only property.
struct TypedGetPropertyView<T> {
/// The name of the property, as a string.
let name : String
/// The actual metatype of the property.
let metatype : T.Type
/// Get the value of the property.
func get() -> T
@tomlokhorst
tomlokhorst / Optional+Unwrap.swift
Last active December 26, 2017 19:50
Unwrap multiple optionals in Swift 1.0
func unwrap<T1, T2>(optional1: T1?, optional2: T2?) -> (T1, T2)? {
switch (optional1, optional2) {
case let (.Some(value1), .Some(value2)):
return (value1, value2)
default:
return nil
}
}
func unwrap<T1, T2, T3>(optional1: T1?, optional2: T2?, optional3: T3?) -> (T1, T2, T3)? {