Skip to content

Instantly share code, notes, and snippets.

View irace's full-sized avatar

Bryan Irace irace

View GitHub Profile
@irace
irace / gist:220d09348fd2a51b07d6
Last active August 29, 2015 14:12
Swift optionals and view controller woes
class CustomViewController: UITableViewController {
// Would love to make this non-optional but I don’t know how to do that when I’m forced to override `init(nibName:bundle:)`
var instance: Class!
// MARK: - Initialization
init(instance: Class) {
super.init(style: .Plain)
self.instance = instance
@irace
irace / gist:509314c46718951137e0
Created November 21, 2014 14:44
How to prevent Core Data objects from being orphaned when their relationship to another object is severed

Say you have a one-to-many relationship (modeled via Core Data) between something like a tweet and a bunch of photos:

@implementation Tweet

- (void)updateWithDictionary:(NSDictionary *)JSONDictionary {
    self.photos = [JSONDictionary["photos"] transformedArrayUsingBlock:^Photo *(NSDictionary *photoJSON) {
      NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Photo" inManagedObjectContext:self.managedObjectContext];
      Photo *photo = [[Photo alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:self.managedObjectContext];
 [photo updateWithDictionary:photoJSON];
@irace
irace / gist:7b22c9a1a3f317589b37
Last active August 29, 2015 14:10
UTIs or MIME types?
// Pros: Possibly more familiar to iOS developers, built-in constants, somewhat more powerful (UTI conformance)
parameters.UTIsToContentRepresentations = @{
(NSString *)kUTTypeHTML: @"<p><b>Hey</b></p>",
@"net.daringfireball.markdown": @"**Hey**"
}
// Pros: (Subjectively) cleaner due to no gross casting, has more widespread usage
parameters.MIMETypesToContentRepresentations = @{
@"text/html": @"<p><b>Hey</b></p>",
@"text/x-markdown": @"**Hey**"
@irace
irace / Stopwatch.swift
Created October 4, 2014 20:55
A simple stopwatch in Swift
import Foundation
/**
* A really simple timer that I didn't want to call `Timer` because timer means something else in Foundation.
*/
@objc class Stopwatch: NSObject {
private var startTime: NSDate?
var isRunning: Bool {
return startTime != nil
@irace
irace / gist:93046a4dc4752af3c8c6
Last active August 29, 2015 14:06
Unit testing gotchas

Stubbing out a method that calls a block instead of returning a value

Googled and found this Stack Overflow answer.

TMAPIClient *APIClient = OCMClassMock([TMAPIClient class]);
    
[hostNamesToBlogNames enumerateKeysAndObjectsUsingBlock:^(NSString *hostName, NSString *blogName, BOOL *stop) {
    OCMStub([APIClient blogInfo:hostName callback:[OCMArg any]]).andDo(^(NSInvocation *invocation) {
 TMAPICallback callback;
@irace
irace / gist:535c9aa3314ee41fb902
Last active April 19, 2018 01:26
Activity items and share extensions

I'm not 100% sure this is all accurate but:

It seems as though share extension predicates are a lot more specific than we originally thought. For example, if Instapaper declares support for only NSExtensionActivationSupportsWebURLWithMaxCount = 1, this means that activity controllers configured with a URL but also some other item types too, e.g. a string, won't show Instapaper.

So basically if you have multiple items in your activity items array, your controller will only show extensions that support all types. I imagine this would be extremely limiting, and as such, think the best option is to:

  • Only include a single provider in your items array. The provider would have a placeholder of a single type that will be provided to any activity or extension that isn't specifically accounted for
  • In the provider, specifically hardcode alternative item types for activities that need an alternative to the placeholder type:
    • "Copy" activity could use photo data, if present
  • "Mail" activity woul
@irace
irace / gist:927f843cb0f683a7f2c2
Created August 13, 2014 20:03
New iOS 8 APIs don't allow you to use a custom presentation controller for compact devices, in conjunction with a popover on "normal" size class devices
//
// ViewController.m
// PopoverPresentationControllerExample
//
// Created by Bryan Irace on 8/8/14.
// Copyright (c) 2014 Bryan Irace. All rights reserved.
//
#import "CustomCompactPresentationController.h"
#import "ViewController.h"
@irace
irace / gist:d4603e87a9405a120a3a
Last active August 29, 2015 14:04
Day One tags (as of 7/17/2014)
  • Food (76)
  • Movies (65)
  • Trips (35)
  • Programming (19)
  • Beer (17)
  • Tracy (17)
  • Cooking (12)
  • Tumblr (12)
  • TV (12)
  • Health (10)
tell application "System Events" to tell process "iOS Simulator"
activate
set frontmost to true
click menu item "Reset Content and SettingsÉ" of menu 0 of menu bar item "iOS Simulator" of menu bar 1
click button "Reset" of window ""
return
end tell
@irace
irace / gist:ccf8e8aa1c4b0e0ce038
Last active August 29, 2015 14:02
Open and scroll to “Quotes” section of mobile Wikiquote website
// When DOM is loaded
$(function () {
var $quotes = $('#Quotes')
, $heading = $quotes.parent('.section_heading');
(function open($element) {
$element.addClass('openSection');
return open;
})($heading)($heading.next('.content_block'));