Skip to content

Instantly share code, notes, and snippets.

View irace's full-sized avatar

Bryan Irace irace

View GitHub Profile
@irace
irace / ConsoleLogDestination.swift
Last active September 20, 2023 08:04
Simple Swift logger
import Foundation
final class ConsoleLogDestination: LogDestination {
func log(statement: String) {
#if DEBUG
print(statement)
#endif
}
func error(error: Error) {
@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 / TabComponent.swift
Last active December 22, 2020 15:38
Easily roll your own `UITabBarController` alternatives. Here’s all the logic you need without assuming anything about your UI.
/**
* A class that can be part of a tabbed navigational interface (expected to be a `UIViewController` but can also be a
* coordinator that proxies through to an underlying controller).
*/
public protocol TabComponent {
/// The tab metadata
var tabItem: TabItem { get }
var viewController: UIViewController { get }
}
@irace
irace / SnapViewController.m
Created March 8, 2014 21:42
A draggable circle that snaps back into place when released, just like the "3kb" circle on http://moofx.mad4milk.net
//
// SnapViewController.m
// DynamicsExample
//
// Created by Bryan Irace on 3/8/14.
// Copyright (c) 2014 Bryan Irace. All rights reserved.
//
#import "SnapViewController.h"
@irace
irace / Codable+JSONObjects.swift
Last active April 17, 2019 15:01
Nearby user discovery through MultipeerConnectivity in Swift
import Foundation
/**
Extensions that allow `JSONEncoder` and `JSONDecoder` to produce/consume dictionaries, instead of simply raw data.
*/
extension JSONEncoder {
func encodeAsJSONObject<T>(_ value: T) throws -> Any where T: Encodable {
return try JSONSerialization.jsonObject(with: try encode(value), options: [])
}
@irace
irace / ObjectToJSON.java
Last active January 12, 2019 08:16
Object-to-JSON string in Java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ObjectToJSON {
public static void main(String[] args) {
Map<Object, Object> birthday = new HashMap<Object, Object>();
birthday.put("day", 12);
birthday.put("month", 8);
@irace
irace / BIWebViewDelegate.m
Created September 10, 2012 02:51
JavaScript/native bridge for iOS's UIWebView
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSString *urlString = [[request URL] absoluteString];
if ([urlString hasPrefix:@"js:"]) {
NSString *jsonString = [[[urlString componentsSeparatedByString:@"js:"] lastObject]
stringByReplacingPercentEscapes];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
@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:3366303
Created August 16, 2012 03:29
Virtualenv cheat sheet
# Install
sudo easy_install virtualenv
# Create a new environment:
cd ~/project
virtualenv env
# Activate
. env/bin/activate