Skip to content

Instantly share code, notes, and snippets.

View jcampbell05's full-sized avatar

James Campbell jcampbell05

View GitHub Profile
@nicklockwood
nicklockwood / gist:21495c2015fd2dda56cf
Last active August 13, 2020 13:57
Thoughts on Swift 2 Errors

Thoughts on Swift 2 Errors

When Swift was first announced, I was gratified to see that one of the (few) philosophies that it shared with Objective-C was that exceptions should not be used for control flow, only for highlighting fatal programming errors at development time.

So it came as a surprise to me when Swift 2 brought (What appeared to be) traditional exception handling to the language.

Similarly surprised were the functional Swift programmers, who had put their faith in the Haskell-style approach to error handling, where every function returns an enum (or monad, if you like) containing either a valid result or an error. This seemed like a natural fit for Swift, so why did Apple instead opt for a solution originally designed for clumsy imperative languages?

I'm going to cover three things in this post:

@mxpr
mxpr / 1-watchkit-update-plists.py
Last active January 14, 2016 23:42
watchkit command line builds
import plistlib
# Read Plists
myAppInfoPlist = plistlib.readPlist(“MyApp/Info.plist”)
watchKitExtensionInfoPlist = plistlib.readPlist(“WatchKitExtension/Info.plist”)
watchKitAppInfoPlist = plistlib.readPlist(“WatchKitApp/Info.plist”)
# Update Watch Kit Extension Plist
watchKitExtensionInfoPlist[“NSExtension”][“NSExtensionAttributes”][“WKAppBundleIdentifier”] = watchKitAppInfoPlist[“CFBundleIdentifier”]
watchKitExtensionInfoPlist[“CFBundleVersion”] = myAppInfoPlist[“CFBundleVersion”]
@scottdelly
scottdelly / FacebookLogin.swift
Last active April 23, 2021 21:43
Facebook Login with iOS SDK 4.0 in Swift
//If you have a Bridging-Header:
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
//In your AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [String: AnyObject]?) -> Bool {
//App launch code
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
//Optionally add to ensure your credentials are valid:
import Cocoa
class GeneralThing<S> {
var stuff: S?
func doIt() {
if let s = stuff {
doWithStuff(s)
}
}
@jspahrsummers
jspahrsummers / GHRunLoopWatchdog.h
Created January 28, 2015 20:50
A class for logging excessive blocking on the main thread
/// Observes a run loop to detect any stalling or blocking that occurs.
///
/// This class is thread-safe.
@interface GHRunLoopWatchdog : NSObject
/// Initializes the receiver to watch the specified run loop, using a default
/// stalling threshold.
- (id)initWithRunLoop:(CFRunLoopRef)runLoop;
/// Initializes the receiver to detect when the specified run loop blocks for
@OmeGak
OmeGak / raetikon.cpp
Last active April 8, 2016 13:26
Decompiled code from Secrets of Raetikon Deserter achievement trigger
function GinkgoGames::SinglePlayerLevel::onQuit() {
// Achievement check begins
rbx = *(rdi + 0x70); // rbx = ID of current scenario
rax = GIN::Symbolize("sp_forst_destructor"); // rax = representation of scenario "sp_forst_destructor"
if (rbx == rax) { // check if we are in scenario "sp_forst_destructor"
rax = GIN::Node::getPropertyValue(
GIN::Node::find(GIN::Symbolize("destructionDirector")), // destructionDirector is the unavoidable mechanism
"Capacitor", // that starts once all 8 shards are plugged in
"killCounter",
"charge",
@alan-mushi
alan-mushi / scrolling_form.c
Created July 11, 2014 12:38
This is a simple example of "scrolling" form with ncurses. It use "page" to allow forms with more fields than your window can print.
/*
* This is a simple example of "scrolling" form with ncurses.
* It use "page" to allow forms with more fields than your window can print.
*
* It prints a "label" (inactive field) and a regular field and let you
* "scroll" pages of the form.
*
* How to compile:
* gcc -o test scrolling_form.c -lform -lncurses
*/
@randomsequence
randomsequence / CIContext+IntermediateImage.m
Created May 12, 2014 12:20
CoreImage - Render a CIImage to an Intermediate CVPixelBuffer Backed Image
@implementation CIContext (IntermediateImage)
- (CIImage *)rsq_renderToIntermediateImage:(CIImage *)image {
CIImage *intermediateImage = nil;
CGSize size = image.extent.size;
CVPixelBufferRef pixelBuffer = NULL;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault,
size.width,
size.height,
kCVPixelFormatType_32ARGB,
@alloy
alloy / Rakefile
Last active March 23, 2016 10:36
A Rakefile that standardises program env installation and guides the user, as opposed to crashing with Ruby exceptions such as `LoadError`. The only case where this will *never* work reliably is if you use the `rubygems-bundler` (NOEXEC) plugin, which introduces chicken-and-egg problems.
# Do *not* load any libs here that are *not* part of Ruby’s standard-lib. Ever.
desc "Install all dependencies"
task :bootstrap do
if system('which bundle')
sh "bundle install"
sh "git submodule update --init"
# etc
else
$stderr.puts "\033[0;31m[!] Please install the bundler gem manually: $ [sudo] gem install bundler\e[0m"
@steipete
steipete / FirstResponder.m
Created January 31, 2014 17:21
Getting the first responder... Unless you want to use the private [view firstResponder] this seems to be the only legal way. Or am I wrong?
UIView *PSPDFFindFirstResponderBeneathView(UIView *view) {
// Stop if e.g. we show an UIAlertView with a text field.
if (UIApplication.sharedApplication.keyWindow != view.window) return nil;
// Search recursively for first responder.
for (UIView *childView in view.subviews) {
if ([childView respondsToSelector:@selector(isFirstResponder)] && childView.isFirstResponder) return childView;
UIView *result = PSPDFFindFirstResponderBeneathView(childView);
if (result) return result;
}