Skip to content

Instantly share code, notes, and snippets.

View klaas's full-sized avatar
🤠
Howdy!

Klaas klaas

🤠
Howdy!
View GitHub Profile
import Foundation
let argumentCount = Int(C_ARGC)
var argumentArray: Array<String> = Array<String>()
for index in 0..argumentCount {
argumentArray.append(String.fromCString(C_ARGV[index]))
}
@tjw
tjw / protocol-type-member.swift
Last active August 29, 2015 14:08
Can't access members of protocol types
public protocol MeasurementUnit {
// If we have one of this unit, how many millimeters is it?
class var asMillimeters: Double { get }
}
public class Inch : MeasurementUnit {
public class var asMillimeters: Double {
get {
return 25.4
}
}
@klaas
klaas / gist:3170952
Created July 24, 2012 16:14
Open a location in Little Locations and let it be displayed in the create tab
// Create location url object
NSURL *url = [NSURL URLWithString:@"littlelocations://create?lat=18.99&lon=72.95&name=Mumbai%20Inner%20Circus"];
// Test if Little Locations is installed
if([[UIApplication sharedApplication] canOpenURL:url] ) {
// Open Location in Little Locations
[[UIApplication sharedApplication] openURL:url];
@mchambers
mchambers / serializer.swift
Created June 25, 2014 07:49
A simple, limited model-to-JSON serializer in Swift.
// Here we'll use Swift's IDE-supporting reflect()
// function to build a basic JSON serializer.
// Per the fine engineers at WWDC, Swift's reflection support
// exists purely to support the IDE and the Playground. But
// we can have some fun with it anyway. ;)
class SerializerBase {
}
@joshtwist
joshtwist / TodoService.m
Last active December 15, 2015 21:29
Simple sample of In-App purchase receipt verification and storage in Mobile Services DB
// this is from the client code - simply pass the transactionReceipt property of the SKPaymentTransaction
// as the receipt parameter to the method below. This will POST the data to the mobile service which will,
// in turn, invoke the insert function on the server shown in this gist
- (void)insertReceipt:(NSData *)receipt completion:(CompletionBlock)completion
{
NSString *string = [[NSString alloc] initWithData:receipt encoding:NSUTF8StringEncoding];
[self.receipts insert:@{ @"receipt" : string } completion:^(NSDictionary *item, NSError *error) {
completion();
}];
}
@OdNairy
OdNairy / main.m
Last active December 21, 2015 00:28
// Source: https://devforums.apple.com/message/866487#866487
typedef int (*PYStdWriter)(void *, const char *, int);
static PYStdWriter _oldStdWrite;
int __pyStderrWrite(void *inFD, const char *buffer, int size)
{
if ( strncmp(buffer, "AssertMacros: queueEntry", 24) == 0 ) {
return 0;
@chriseidhof
chriseidhof / gcd.swift
Created August 28, 2014 01:08
GCD Wrappers
import Foundation
// Executes an array of blocks in parallel, but only returns after they're all done.
func parallel(blocks: [() -> ()]) {
let group = dispatch_group_create()
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
for block in blocks {
dispatch_group_async(group, queue, block)
}
@klaas
klaas / StreamReader.swift
Last active May 21, 2017 14:51
Xcode 6.1 version of source for this SO answer http://stackoverflow.com/a/24648951/292145
import Foundation
class StreamReader {
let encoding : UInt
let chunkSize : Int
var atEof : Bool = false
var fileHandle : NSFileHandle!
let buffer : NSMutableData!
@frozzare
frozzare / array.swift
Created June 8, 2014 18:43
Example how to extend array type in Swift
import Foundation
extension Array {
/**
* Get first item in array.
*
* @return First item in array.
*/
@jder
jder / Downscaling ALAssets
Last active March 20, 2018 06:51
A method for downscaling ALAssets
// For details, see http://mindsea.com/2012/12/18/downscaling-huge-alassets-without-fear-of-sigkill
#import <AssetsLibrary/AssetsLibrary.h>
#import <ImageIO/ImageIO.h>
// Helper methods for thumbnailForAsset:maxPixelSize:
static size_t getAssetBytesCallback(void *info, void *buffer, off_t position, size_t count) {
ALAssetRepresentation *rep = (__bridge id)info;
NSError *error = nil;