Skip to content

Instantly share code, notes, and snippets.

View fdstevex's full-sized avatar

stevex fdstevex

View GitHub Profile
@fdstevex
fdstevex / numberFromFractionalString
Last active December 18, 2015 13:39
This is an Objective-C function that will parse a float from a string where the fractional part is expressed as numerator/denominator. So, for example, it will parse "1 1/2", or "3/4".
/**
* Turn a string like "1 1/2" into 1.5. If there is no fractional
* part, then the whole number is returned. If the number is not
* well formatted, then nil is returned.
*/
+ (NSNumber *)numberFromFractionalString:(NSString *)string
{
if (string == nil) {
return nil;
}
@fdstevex
fdstevex / gist:6557526
Last active December 23, 2015 00:59
Simple Core Data example showing how long it takes to create 10,000 records and then update those records.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *db = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TestDB.sqlite"];
[[NSFileManager defaultManager] removeItemAtURL:db error:nil];
NSManagedObjectContext *moc = [self managedObjectContext];
NSLog(@"Creating Entities");
for (int i=0; i<10000; i++) {
@fdstevex
fdstevex / gist:6741638
Created September 28, 2013 12:34
Method to retrieve a list of downloadable fonts on iOS 7. This method may block for a while (it can involve a network call) so don't call on main thread. The return is a dictionary that maps font families to arrays of font names in that family.
+ (NSDictionary *)downloadableFonts
{
CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)@{(id)kCTFontDownloadableAttribute : (id)kCFBooleanTrue});
CFArrayRef matchedDescs = CTFontDescriptorCreateMatchingFontDescriptors(desc, NULL);
NSArray *array = (__bridge NSArray *)matchedDescs;
NSMutableDictionary *families = [NSMutableDictionary dictionary];
for (UIFontDescriptor *fontDescriptor in array) {
NSString *familyName = fontDescriptor.fontAttributes[UIFontDescriptorFamilyAttribute];
@fdstevex
fdstevex / gist:285ca51c5ae487fb2e6b
Created May 30, 2014 19:37
AngularJS: Submitting JSON using a MIME POST
return $http({
method: "POST",
url: "/2/projects?accessToken=" + $userService.accessToken,
// The undefined Content-Type is intentional; with that, the browser will genearate
// the MIME boundaries and set the content type later.
headers: {'Content-Type': undefined },
transformRequest: function (data) {
var formData = new FormData();
formData.append("project", angular.toJson(postVars));
return formData;
@fdstevex
fdstevex / gist:af1c0a13a5e6ce696bf0
Last active August 3, 2017 16:31
Auto layout in playground
// Auto layout in a Swift Playground (for iOS).
import UIKit
var v = UIView()
v.frame = CGRectMake(0, 0, 200, 200)
var b1 = UIButton()
b1.setTitle("Click Me", forState:UIControlState.Normal)
b1.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
@fdstevex
fdstevex / Diff.xcplayground
Created June 15, 2015 12:12
Xcode Playground showing a Swift diffing algorithm suitable for performing delta updates to a WKInterfaceTable
// arr1 is the current list
// arr2 is the new list
// the insert and delete callbacks will be called - pass these
// through to WKInterfaceTable to perform a delta update
func diff<T: Comparable>(arr1: Array<T>, var arr2: Array<T>, insertFunc: (index: Int, item: T) -> Void, deleteFunc: (Int) -> Void) {
var idx1 = 0
var idx2 = 0
while (idx1 < arr1.count) {
// identical items; step to next one
if (idx2 < arr2.count && arr1[idx1] == arr2[idx2]) {
@fdstevex
fdstevex / checkfront_to_slack.php
Created March 19, 2016 13:02
CheckFront Booking to Slack Notification PHP Script
<?php
// (string) $message - message to be passed to Slack
// (string) $room - room in which to write the message, too
// (string) $icon - You can set up custom emoji icons to use with each message
function slack($message, $room = "general", $icon = ":bell:") {
$room = ($room) ? $room : "general";
$data = "payload=" . json_encode(array(
"channel" => "#{$room}",
"text" => $message,
@fdstevex
fdstevex / AVURLAsset.audiovisualMIMETypes()
Created May 13, 2016 11:03
Output from AVURLAsset.audiovisualMIMETypes for easy reference.
(lldb) p types
([String]) $R0 = 56 values {
[0] = "video/mp4"
[1] = "video/x-m4v"
[2] = "video/mpg"
[3] = "audio/x-m4r"
[4] = "audio/AMR"
[5] = "video/x-mpg"
[6] = "video/3gpp2"
[7] = "video/mp2p"
@fdstevex
fdstevex / allbonjour.swift
Created May 15, 2016 02:32
Swift command-line script to find Bonjour services on your local network.
#!/usr/bin/swift
// Swift script to search for Bonjour services on the local network.
// Found services are printed to the console.
import Foundation
import CoreFoundation
// Inner search, finds services of a particular type
@fdstevex
fdstevex / SPVideoRecorder.h
Last active May 23, 2016 02:18
SPVideoRecorder from http://stackoverflow.com/questions/6795157/what-is-a-10-6-compatible-means-of-recording-video-frames-to-a-movie-without-usi updated with changes required for ffmpeg libavformat circa ffmpeg 3.0.1
#import <Foundation/Foundation.h>
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include <libavutil/imgutils.h>
uint64_t getNanoseconds(void);