Skip to content

Instantly share code, notes, and snippets.

View billibala's full-sized avatar

Bill So billibala

View GitHub Profile
@billibala
billibala / Date.swift
Created May 22, 2018 04:07
Demonstrates run time error when the bounds of a date range is not in ascending order
//: Playground - noun: a place where people can play
import Cocoa
let now = Date()
let earlierDate = Date(timeIntervalSinceNow: -2 * 24 * 3600)
switch Date() {
case now..<earlierDate:
print("hello")
@billibala
billibala / schema.json
Created May 5, 2018 18:14
Improved schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "",
"type": "object",
"patternProperties": {
"^(price|barcode|sku)$": {
"anyOf":[
{
"type": "string"
},
@billibala
billibala / response.json
Last active May 5, 2018 16:04
JSON validation example
{
"name":"Product name",
"price": 876,
"barcode": "908w839827983742973",
"sku": "hello",
"another": "just a value"
}
@billibala
billibala / gist:50bd2620bc00692100e1
Last active August 29, 2015 14:07
NSSplitView - how to programmatically hide or collapse a subview of a split view
// if it's a horizontal divider...
[_splitView setPosition:_splitView.bounds.size.height ofDividerAtIndex:0];
// Make sure NSSplitViewDelegate implements "- (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview"
codesign --display --entitlements - Atttach.app
Don't forget the "-" in the command
@billibala
billibala / XCTestAsync.mm
Created October 10, 2013 10:51
Call "waitForCompletion" after invoking an async method. Upon receiving notification, set "executionDate" to YES and call [runloopExpirationTimer fire]. This will "dry up" the fake run loop.
- (void)waitForCompletion {
runloopExpirationTimer = [NSTimer scheduledTimerWithTimeInterval:600.0 target:self selector:@selector(dumbTimerMethod:) userInfo:nil repeats:NO];
while ( !executionDone ) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}
- (void)dumbTimerMethod:(NSTimer *)theTimer {
runloopExpirationTimer = nil;
}
@billibala
billibala / FocusRing.html
Created July 30, 2013 02:08
Fake a Mac OS X focus ring for DOM element in a web view
<html>
<head>
<title>Focus ring</title>
<meta name="author" content="Headnix">
<style type="text/css" media="screen">
.thumbnail {
width: 100px;
height: 100px;
background-color: #eee;
}
@billibala
billibala / DocumentPackageCheck.mm
Last active December 20, 2015 01:39
Mac/iOS - Checks whether a file URL is a document package, regular or a folder.
// Assume base path is your Desktop
NSURL * baseFileURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDesktopDirectory inDomains:NSUserDomainMask] lastObject];
NSURL * fileURL = [baseFileURL URLByAppendingPathComponent:@"__FILE_NAME__"];
// the keys we are interested in
NSArray * attrArray = @[NSURLIsPackageKey, NSURLIsRegularFileKey, NSURLIsWritableKey, NSURLIsDirectoryKey];
NSDictionary * theDict = [fileURL resourceValuesForKeys:attrArray error:nil];
NSLog(@"the dict: %@", theDict);