Skip to content

Instantly share code, notes, and snippets.

View bryanluby's full-sized avatar

Bryan Luby bryanluby

  • Palatine, IL USA
View GitHub Profile
@bryanluby
bryanluby / gist:548beba8f8062c81ed68
Last active July 28, 2016 14:57
Static variable inside of a function in Swift.
func staticInsideFunction() {
struct Temp { static var hasAnimated = false }
if Temp.hasAnimated == false {
// ... do something only on the first function call.
Temp.hasAnimated = true
} else {
// ... do something on all subsequent function calls.
}
}
@bryanluby
bryanluby / gist:fd5ac0a8c74c2c3241d9
Last active June 1, 2023 16:10
Switch on indexPath using tuple example in Swift.
switch (indexPath.section, indexPath.row) {
case (0, _): println("section 0")
case (1, _): println("section 1")
case (2, 0...2): println("section 2 0-2")
case (2, 3...5): println("section 2 3-5")
case (3, let row): println("\(row)")
default: println("")
}
@bryanluby
bryanluby / NSObject+LUBDescription.h
Created May 6, 2014 01:04
NSObject category for describing the keys and values of an object.
//
// NSObject+LUBDescription.h
//
// Created by Bryan Luby on 5/5/14.
// Copyright (c) 2014 Bryan Luby. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSObject (LUBDescription)
@bryanluby
bryanluby / gist:9159185
Last active August 29, 2015 13:56
Create arbitrary key-value pairs for an object (similar to kvc compliant container classes like CALayer).
@interface CustomObject ()
@property (strong, nonatomic) NSMutableDictionary *arbitraryKeyValuePairs;
@end
@implementation CustomObject
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
if (!self.arbitraryKeyValuePairs) {
self.arbitraryKeyValuePairs = [NSMutableDictionary dictionary];
@bryanluby
bryanluby / gist:9157978
Created February 22, 2014 16:54
CALayer helper category for nib access through user defined runtime attributes.
@implementation CALayer (NibAccess)
- (void)setLub_borderColor:(UIColor *)color
{
self.borderColor = color.CGColor;
}
- (UIColor *)lub_borderColor
{
return [UIColor colorWithCGColor:self.borderColor];
@bryanluby
bryanluby / gist:9085761
Created February 19, 2014 03:51
Handle UITextView adjustments when showing and hiding the keyboard.
- (void)handleKeyboardShow:(NSNotification *)notification
{
NSDictionary *dict = notification.userInfo;
NSValue *rectValue = dict[UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [rectValue CGRectValue];
UITextView *activeTextView;
if ([self.textView1 isFirstResponder]) {
activeTextView = self.textView1;
} else if ([self.textView2 isFirstResponder]) {
@bryanluby
bryanluby / gist:8773920
Last active March 2, 2017 08:15
Resign first responder from anywhere.
// Make sure viewDidAppear has happened before sending this action to ensure that all links
// in the responder chain are connected.
// http://stackoverflow.com/a/11768282/1306872
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
to:nil
from:nil
forEvent:nil];
@bryanluby
bryanluby / gist:8772880
Last active August 29, 2015 13:55
Inspect the responder chain
// http://stackoverflow.com/a/4242037/1306872
- (void)inspectResponderChainFromSender:(id)sender
{
if ([sender isKindOfClass:[UIResponder class]]) {
NSMutableArray *responderChain = [NSMutableArray array];
[responderChain insertObject:sender atIndex:0];
while ((sender = [sender nextResponder])) {
if ([sender nextResponder]) {
@bryanluby
bryanluby / NSURL+Tags.h
Created February 1, 2014 17:23
NSURL category to get and set Finder tags.
//
// NSURL+Tags.h
//
// Created by Bryan Luby on 10/23/13.
// Copyright (c) 2013 Bryan Luby. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSURL (Tags)
@bryanluby
bryanluby / NSString+Reversed.h
Created February 1, 2014 17:17
NSString category for reversing a string.
//
// NSString+Reversed.h
//
// Created by Bryan Luby on 5/16/13.
//
#import <Foundation/Foundation.h>
@interface NSString (Reversed)