Skip to content

Instantly share code, notes, and snippets.

@nigelbrady
nigelbrady / NSDataPackets.swift
Created September 2, 2015 22:48
Split NSData into custom-sized packets.
extension NSData{
func dataPacketsWithMaximumSize(size: Int) -> [NSData]{
if self.length <= size{
return [self]
}
var result: [NSData] = []
var index = 0
@nigelbrady
nigelbrady / TapCharacterController.cs
Last active April 6, 2023 11:35
Simple Tap-To-Move Character Controller for Unity
using UnityEngine;
using System.Collections;
using System.Linq;
public class TapCharacterController : MonoBehaviour
{
public delegate void MotionEventHandler (GameObject sender);
public delegate void TargetHandler (GameObject sender,GameObject target);
@nigelbrady
nigelbrady / RemoveOldest.m
Created December 24, 2014 16:44
Remove the oldest file in an iOS Directory
//Taken from: http://stackoverflow.com/questions/15747777/delete-oldest-file-in-directory-ios
+(void)removeOldestFileFromDir:(NSString *)path{
NSError *error = nil;
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *contents = [fm contentsOfDirectoryAtPath:path error:&error];
NSDate *oldest = [NSDate date];
NSString *oldestFileName = nil;
for (NSString *f in contents) {
NSString *filePath = [path stringByAppendingPathComponent:f];
@nigelbrady
nigelbrady / folderSize.m
Created December 24, 2014 16:23
Size of an iOS File Directory
//Taken from: http://stackoverflow.com/questions/2188469/calculate-the-size-of-a-folder
- (unsigned long long int)folderSize:(NSString *)folderPath {
NSArray *filesArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:folderPath error:nil];
NSEnumerator *filesEnumerator = [filesArray objectEnumerator];
NSString *fileName;
unsigned long long int fileSize = 0;
while (fileName = [filesEnumerator nextObject]) {
NSDictionary *fileDictionary = [[NSFileManager defaultManager] fileAttributesAtPath:[folderPath stringByAppendingPathComponent:fileName] traverseLink:YES];
@nigelbrady
nigelbrady / OptionalOperators.swift
Created December 24, 2014 03:05
Convenient Optional Operators for Swift
//This postfix operator unwraps an Optional, and if it is nil, returns the default value for that type.
postfix operator *! {}
postfix func *! (opt: String?) -> String{
return opt == nil ? "" : opt!
}
postfix func *! (opt: Int?) -> Int{
return opt == nil ? 0 : opt!
@nigelbrady
nigelbrady / flashScreen.m
Created December 23, 2014 03:58
iOS-Flash Screen
//Taken from: http://stackoverflow.com/questions/12924094/simulate-a-picture-taken-screen-flash
-(void)flashScreen{
// Flash the screen white and fade it out
UIView *flashView = [[UIView alloc] initWithFrame:self.view.frame];
[flashView setBackgroundColor:[UIColor whiteColor]];
[[[self view] window] addSubview:flashView];
[UIView animateWithDuration:1.f
animations:^{
#import <CoreLocation/CoreLocation.h>
#import <ImageIO/ImageIO.h>
@interface CLLocation (EXIFGPS)
- (NSDictionary*) EXIFMetadataWithHeading:(CLHeading*)heading;
@end
@interface NSDate (EXIFGPS)
@nigelbrady
nigelbrady / python-haversine
Last active November 11, 2016 19:32
Python Haversine Formula for Calculating GPS Distance
from math import radians, cos, sin, asin, sqrt
#Taken from http://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
@nigelbrady
nigelbrady / bind_request_attributes
Created December 12, 2014 04:40
How to bind webapp2 requests to a model quickly...
class Handler(webapp2.RequestHandler):
def get(self):
x = SomeClass()
#This...
self.bind_request_attributes(x, attr_list=["attr1", "attr2", "attr3"])
#Is equal to this
attr1 = self.request.get("attr1")
@nigelbrady
nigelbrady / SpriteKit-Animation
Last active August 29, 2015 14:04
Simple SpriteKit Animation with SKAction.
-(void)animateSquare:(SKSpriteNode *) node
withIndex: (int)index
andColor:(SKColor *) color
rounds:(int)rounds
duration: (NSTimeInterval) duration
{
CGSize newSize = [self sizeForSquareWithNumberOfRounds:rounds];
CGPoint center = CGPointMake(CGRectGetMidX(self.boardFrame) - newSize.width/2,