Skip to content

Instantly share code, notes, and snippets.

View rcdilorenzo's full-sized avatar
🙌
Working in His Kingdom (Col 3:17)

Christian Di Lorenzo rcdilorenzo

🙌
Working in His Kingdom (Col 3:17)
View GitHub Profile
@rcdilorenzo
rcdilorenzo / UIViewSubclass.m
Created October 23, 2013 13:36
A piece of code for doing auto layout in a UIView subclass
// Declare this property in your header / private header
@property (nonatomic, strong) NSMutableArray *updatingConstraints;
- (void)updateConstraints {
if (self.updatingConstraints) {
[self removeConstraints:self.updatingConstraints];
}
self.updatingConstraints = [NSMutableArray array];
// Create and add all of your constraints here
#import <Foundation/Foundation.h>
@interface NSObject (Swizzling)
- (void)swizzleMethod:(SEL)originalSelector toMethod:(SEL)newSelector;
@end
@rcdilorenzo
rcdilorenzo / gist:7169910
Created October 26, 2013 14:14
Animating constraints in the method "updateConstraints"
- (void)animateConstraints {
[self setNeedsUpdateConstraints];
[UIView animateWithDuration:0.4 animations:^{
[self layoutIfNeeded];
}];
}
@rcdilorenzo
rcdilorenzo / comments.txt
Created December 11, 2013 20:37
Unique programmer comments
long long ago; /* in a galaxy far far away */
// Replaces with spaces the braces in cases where braces in places cause stasis
$str = str_replace(array("\{","\}")," ",$str);
#define TRUE FALSE //Happy debugging suckers
// sometimes I believe compiler ignores all my comments
// I am not sure if we need this, but too scared to delete.
@rcdilorenzo
rcdilorenzo / KIFUITestActor+Generic.m
Created December 12, 2013 16:11
A KIF extension to allow tapping a cell based on its name
- (void)tapCellWithName:(NSString *)name inTableViewWithAccessibilityLabel:(NSString *)accessibilityLabel {
[tester runBlock:^KIFTestStepResult(NSError *__autoreleasing *error) {
UITableView *tableView = (UITableView *)[tester waitForViewWithAccessibilityLabel:accessibilityLabel];
NSIndexPath *foundIndexPath = nil;
for (int section = 0; section < tableView.numberOfSections; section++) {
for (int row = 0; row < [tableView numberOfRowsInSection:section]; row++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell.textLabel.text isEqualToString:name]) {
foundIndexPath = indexPath;
@rcdilorenzo
rcdilorenzo / CentroidCalculator.m
Created February 1, 2014 18:40
Simple Obj-C function to calculate centroid of points
CGPoint centroidOfPoints(const CGPoint* points, int pointCount) {
CGPoint centroid = CGPointMake(0, 0);
double x0, y0, x1, y1, partialArea;
double signedArea = 0.0;
for (int index = 0; index < pointCount; index++) {
int nextIndex = (index == pointCount-1) ? 0 : index+1;
x0 = points[index].x;
y0 = points[index].y;
@rcdilorenzo
rcdilorenzo / json_map_builder.ex
Last active October 25, 2021 08:19
Convert a elixir json-decoded object to a map no matter how deep
defmodule JSONMapBuilder do
def to_map(list) when is_list(list) and length(list) > 0 do
case list |> List.first do
{_, _} ->
Enum.reduce(list, %{}, fn(tuple, acc) ->
{key, value} = tuple
Map.put(acc, binary_to_atom(key), to_map(value))
end)
_ ->
list
defmodule Controller.Main do
use Weber.Controller
render_when_raise :unauthorized, {:text, 401, "Unauthorized.", []}
layout false
def action(_, conn) do
{:render, [project: "simpleTodo"], []}
import Foundation
extension Int {
func timesMap(closure: (Int) -> AnyObject) -> Array<AnyObject> {
var array: AnyObject[] = []
for x in 1...self {
array += closure(x)
}
return array;
}
@rcdilorenzo
rcdilorenzo / macro_fun.exs
Created January 30, 2015 03:27
Macro fun in Elixir mimicking Ruby's attr_accessor
defmodule MacroExp do
defmacro attr_accessor(atom) do
getter = String.to_atom("get_#{atom}")
setter = String.to_atom("set_#{atom}")
quote do
def unquote(getter)(data) do
data |> Map.from_struct |> Map.get(unquote(atom))
end
def unquote(setter)(data, value) do
data |> Map.put(unquote(atom), value)