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:6615028
Last active December 23, 2015 09:29
Objc: Simple code timing block
NSTimeInterval start = [[NSDate date] timeIntervalSince1970];
// Code to execute.
NSTimeInterval finish = [[NSDate date] timeIntervalSince1970];
NSLog(@"Execution took %f seconds.", finish - start);
@bryanluby
bryanluby / gist:6615420
Last active December 23, 2015 09:38
Objc: Alternate the background color of a UITableViewCell.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0) {
UIColor *altCellColor = [UIColor colorWithWhite:0.7 alpha:0.1];
cell.backgroundColor = altCellColor;
}
}
@bryanluby
bryanluby / gist:6615506
Last active December 23, 2015 09:38
Objc: Autolayout debugger trace
// Pause app and enter into debugger
// Any ambiguous layouts will be labeled
po [[UIWindow keyWindow] _autolayoutTrace]
@bryanluby
bryanluby / gist:6615647
Created September 18, 2013 21:01
Objc: Switch with ternary operator
void ConditionalSwitchUsingNumber(int number);
int main(int argc, const char * argv[])
{
@autoreleasepool
{
ConditionalSwitchUsingNumber(0);
ConditionalSwitchUsingNumber(1);
ConditionalSwitchUsingNumber(2);
ConditionalSwitchUsingNumber(6);
@bryanluby
bryanluby / CdToXcodeProject.txt
Last active December 25, 2015 10:58
Change Terminal directory to frontmost Xcode project window.
# Works best when an Xcode project is already open.
# Full Terminal command:
cd `osascript -e "tell application \"Xcode\" to get project directory of front project"`; pwd;
# Optional: Add this as an alias to .bash_profile. Then type "xt" in Terminal to invoke the command.
alias xt='cd `osascript -e "tell application \"Xcode\" to get project directory of front project"`; pwd;'
@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)
@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 / 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 / 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: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]) {