Skip to content

Instantly share code, notes, and snippets.

View kristopherjohnson's full-sized avatar
💭
Huh?

Kristopher Johnson kristopherjohnson

💭
Huh?
View GitHub Profile
@kristopherjohnson
kristopherjohnson / NSJSONSerialization+KDJJSONFiles.h
Last active August 29, 2015 13:56
NSJSONSerialization category to read a JSON object from a file or write a JSON object to a file
#import <Foundation/Foundation.h>
@interface NSJSONSerialization (KDJJSONFiles)
// Returns a Foundation object from JSON data in given file
+ (id)kdj_JSONObjectWithFileAtPath:(NSString *)path options:(NSJSONReadingOptions)options error:(NSError **)error;
// Write JSON object to file at specified path
//
// Returns number of bytes written to the file, or 0 if an error occurs
@kristopherjohnson
kristopherjohnson / KDJUnitTestAppDelegate.m
Last active August 29, 2015 13:56
Display a special iOS unit-testing view controller when running unit tests, rather than constructing the real UI
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSDictionary *env = [[NSProcessInfo processInfo] environment];
// This environment variable is set by the scheme when the app is being launched for unit testing
if (env[@"KDJ_IsRunningUnitTests"] != nil) {
// Show "Running Unit Tests" view when running tests, rather than initializing the app
self.window.rootViewController = [[KDJUnitTestViewController alloc] init];
[self.window makeKeyAndVisible];
@kristopherjohnson
kristopherjohnson / CoreDataOperationsCategory.txt
Created February 28, 2014 14:04
TextExpander snippet for a category for a generated Core Data class. Fill in name of class and name of entity.
// %filltext:name=ClassName:default=ManagedObject%+Operations.h
// (for entity %filltext:name=EntityName:default=Entity%)
#import "%filltext:name=ClassName:default=ManagedObject%.h"
// Category of operations on %filltext:name=ClassName:default=ManagedObject% that are not automatically generated.
//
// Defining these operations in a category lets us re-generate %filltext:name=ClassName:default=ManagedObject%.h and
// %filltext:name=ClassName:default=ManagedObject%.m whenever the schema changes.
@interface %filltext:name=ClassName:default=ManagedObject% (Operations)
@kristopherjohnson
kristopherjohnson / asyncsem.m
Last active August 29, 2015 13:57
Snippet for using a semaphore to signal completion of an asynchronous operation
dispatch_semaphore_t asyncComplete = dispatch_semaphore_create(0);
// TODO: Replace this with asynchronous code. Call dispatch_semaphore_signal(asyncComplete) on completion
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_semaphore_signal(asyncComplete);
});
// Wait for completion or timeout
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:<#timeoutSeconds#>];
while (dispatch_semaphore_wait(asyncComplete, DISPATCH_TIME_NOW) && ([[NSDate date] timeIntervalSinceDate:timeoutDate] < 0)) {
@kristopherjohnson
kristopherjohnson / KDJCoreDataUsingThing.m
Last active August 29, 2015 13:57
Snippet to set up Core Data stack with private context and main-queue context that is updated whenever private context is saved
@interface KDJCoreDataUsingThing ()
// Private-queue context used to modify persistent store
@property (nonatomic) NSManagedObjectContext *privateContext;
// Main-queue context used by user interface
@property (nonatomic) NSManagedObjectContext *mainQueueContext;
@end
@kristopherjohnson
kristopherjohnson / appVersionString.m
Last active August 29, 2015 13:57
Returns a NSString like "Version 1.2 (20140303)", with values read from app bundle
- (NSString *)appVersionString {
NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
NSString *shortVersionString = info[@"CFBundleShortVersionString"];
NSString *bundleVersion = info[@"CFBundleVersion"];
return [NSString stringWithFormat:NSLocalizedString(@"Version %@ (%@)", nil),
shortVersionString, bundleVersion];
}
@kristopherjohnson
kristopherjohnson / NSManagedObjectContext+KDJExistingObjects.h
Created March 11, 2014 16:32
Convenience method to fetch multiple objects from a managed object context by objectID
#import <CoreData/CoreData.h>
@interface NSManagedObjectContext (KDJExistingObjects)
// Returns objects with specified object IDs. Returns nil if any of the objects cannot be fetched or do not exist.
- (NSArray *)kdj_existingObjectsWithIDs:(NSArray *)objectIDArray error:(NSError **)error;
@end
@kristopherjohnson
kristopherjohnson / 0_reuse_code.js
Created April 3, 2014 13:51
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@kristopherjohnson
kristopherjohnson / mac_scanner.md
Created April 7, 2014 16:53
Creating PDF from scanned documents on OS X using Canon MF8300C scanner
  1. Turn MF8300C on, press the Scan button on the console, and select Remote Scanner from the menu
  2. Start Preview.app
  3. Choose File -> Import from Scanner -> Canon MF8300C Series
  4. Put page(s) on scanner face up.
  5. Click the Scan button in the Import from Canon MF8300C dialog.
  6. Pages will appear in a new Preview document
  7. Select all pages
  8. File -> Export as PDF...
@kristopherjohnson
kristopherjohnson / LogUtil.java
Last active August 29, 2015 14:00
Android logging methods that check Log.isLoggable()
package net.kristopherjohnson.util;
import android.util.Log;
/**
* Static utility methods related to logging, using the android.util.Log class.
*
* All application classes should use the methods of this class rather than using
* android.util.Log directly, to allow application-level customization of the logging mechanism.
*/