Skip to content

Instantly share code, notes, and snippets.

View bnickel's full-sized avatar

Brian Nickel bnickel

View GitHub Profile
@bnickel
bnickel / KIFTestActor-BruteForceEscape.m
Created September 16, 2013 03:40
A step to tap wildly on different buttons while trying to get your app to a final state. For example, if your home screen has a "Login" button, your logout flow consists of tapping "Logout" and "Confirm", and your tests could fail leaving alerts with "OK" or "Continue", you could use `[tester tapViewsWithAccessibilityLabels:@[@"OK", @"Continue",…
@implementation KIFUITestActor (BruteForceEscape)
- (void)tapViewsWithAccessibilityLabels:(NSArray *)labels whileWaitingForViewWithAccessibilityLabel:(NSString *)targetLabel
{
[self runBlock:^KIFTestStepResult(NSError **error) {
for (NSString *label in labels) {
UIView *view;
UIAccessibilityElement *element;
@bnickel
bnickel / KIFTestActor-TableView.m
Created October 6, 2013 05:50
[tester removeAllItemsFromTableView] will remove all items from a UITableView managed by a UITableViewController where the "Edit" button is exposed.
@implementation KIFUITestActor (TableView)
- (BOOL)deleteAnyVisibleTableViewCell
{
UIAccessibilityElement *element = [[UIApplication sharedApplication] accessibilityElementMatchingBlock:^BOOL(UIAccessibilityElement *element) {
return [element.accessibilityLabel hasPrefix:@"Delete "];
}];
if (!element) {
return NO;
}
@bnickel
bnickel / myFilter
Created June 11, 2014 16:53
Example filter implementation in Swift.
import Foundation
struct FilteringGenerator<T where T:Generator> : Generator {
let test:(T.Element) -> Bool
var generator:T
init(_ generator:T, _ test: (T.Element) -> Bool) {
self.test = test
self.generator = generator
}
@bnickel
bnickel / PrimeEnumerator.m
Last active August 29, 2015 14:02
Infinite prime sequences in Objective-C and Swift
@import Foundation;
@interface PrimeEnumerator : NSObject<NSFastEnumeration>
- (NSArray *)knownPrimes;
@end
@interface PrimeEnumerator ()
@property (nonatomic, strong) NSMutableArray *primes;
@end
// Good idea or Bad idea?
// Lets say you have to call some API with lots of parameters that you don't control
// Like this http://developer.android.com/reference/android/text/StaticLayout.html#StaticLayout(java.lang.CharSequence, int, int, android.text.TextPaint, int, android.text.Layout.Alignment, float, float, boolean, android.text.TextUtils.TruncateAt, int)
// Do you prefer Option #1 or Option #2? Is Option #2 a terrible idea?
// I suppose Option #3 could be to wrap it in a Builder
class Foo {
@bnickel
bnickel / ConstraintPlayground.swift
Created September 15, 2014 17:28
Shorthand declaration of NSLayoutConstraint without visual format. (For those one-off cases where you don't want to use the visual format language.)
import Cocoa
struct PartialConstraint {
private let item:AnyObject
private let attribute:NSLayoutAttribute
private let multiplier:CGFloat?
private let constant:CGFloat?
private func constraintWith(partial:PartialConstraint, relation:NSLayoutRelation) -> NSLayoutConstraint {
assert(multiplier == nil && constant == nil, "Cannot define multiplier or constant on LHS.")
@bnickel
bnickel / UIView+BadBackgroundBehavior.m
Last active August 8, 2016 01:20
Sometimes, it's just worth it to accept the risk and do UIKit operations on a background thread; you just have to manage those risks. This category prints to the log any time you accidentally mess with setAnimationsEnabled: from a background thread and lets you set a breakpoint at SEViewAlertForUnsafeBackgroundCalls to inspect the call stack. Yo…
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#ifdef DEBUG
void SEViewAlertForUnsafeBackgroundCalls() {
NSLog(@"----------------------------------------------------------------------------------");
NSLog(@" ");
NSLog(@"Background call to setAnimationsEnabled: detected. This method is not thread safe.");
NSLog(@"Set a breakpoint at SEUIViewDidSetAnimationsOffMainThread to inspect this call.");
@bnickel
bnickel / SEUILeftInsetFromRightButton.swift
Created October 15, 2014 18:33
A simple button where the left inset is adjusted to a fixed value, pinning the content to the right edge. Used in Stack Exchange.app to have a variable width empty button with an icon on the right side. Uses IBDesignable and IBInspectable so we never have to reference the button in code. :)
import UIKit
@IBDesignable
class SEUILeftInsetFromRightButton: UIButton {
@IBInspectable var leftInsetFromRight:CGFloat = 0 {
didSet {
updateContentEdgeInsetsWithWidth(CGRectGetWidth(bounds))
}
}
@bnickel
bnickel / fix-xcode
Last active August 29, 2015 14:08 — forked from rnapier/fix-xcode
#!/usr/bin/python
# fix-xcode
# Rob Napier <robnapier@gmail.com>
# Script to link in all your old SDKs every time you upgrade Xcode
# Create a directory called /SDKs (or modify source_path).
# Under it, put all the platform directories:
# MacOSX.platform iPhoneOS.platform iPhoneSimulator.platform
# Under those, store the SDKs:
@bnickel
bnickel / bad-error-messages
Created December 26, 2014 19:39
Demonstrates NSURLSession's lack of localized error descriptions.
#!/usr/bin/swift
import Foundation
let url = NSURL(string: "http://not-a-real-host")!
let request = NSURLRequest(URL: url)
var response:NSURLResponse? = nil
var error:NSError? = nil
NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)