Skip to content

Instantly share code, notes, and snippets.

View jpmhouston's full-sized avatar

Pierre Houston jpmhouston

View GitHub Profile
@jpmhouston
jpmhouston / NSObject+Backtrace.h
Created February 7, 2014 22:08
Backtrace category on NSObject
@interface NSObject (Backtrace)
+ (NSString *)appBacktraceOfDepth:(int)depth fromStackSymbols:(NSArray *)frames;
+ (NSString *)backtraceOfDepth:(int)depth fromStackSymbols:(NSArray *)frames;
+ (NSString *)backtraceOfDepth:(int)depth fromStackSymbols:(NSArray *)frames matching:(NSString *)from;
@end
#define BACKTRACE_DEPTH(DEPTH) ([[NSThread class] respondsToSelector:@selector(callStackSymbols)] ? [NSObject appBacktraceOfDepth:DEPTH fromStackSymbols:[NSThread callStackSymbols]] : @"")
#define CALLSTACK_DEPTH(DEPTH) BACKTRACE_DEPTH(DEPTH)
@jpmhouston
jpmhouston / JPHAsyncOperation.h
Created September 6, 2014 04:42
NSOperation subclass implementing +asyncOperationWithBlock:
#import <Foundation/Foundation.h>
@interface AsyncOperation : NSOperation
@property (nonatomic, assign) BOOL isExecuting; // yes, explicity not '.. getter=isExecuting) BOOL executing'
@property (nonatomic, assign) BOOL isFinished; // the KVC keys must be isExecuting & isFinished, with the 'is'
typedef void (^FinishBlock)(void);
+ (instancetype)asyncOperationWithBlock:(void (^)(FinishBlock finishBlock))block;
@implementation NSDictionary (MapDictKeysContainingPeriods)
- (NSDictionary *)dictionaryWithPeriodsInKeysMappedTo:(NSString *)replacement
{
NSMutableDictionary *updatedDictionary = [self mutableCopy];
NSAssert1(updatedDictionary != self, @"mutableCopy returned same object pointer: %@", updatedDictionary); // i don't think this ever happens
BOOL changed = NO;
for (NSString *key in updatedDictionary.allKeys)
{
id value = [updatedDictionary objectForKey:key];
@jpmhouston
jpmhouston / gist:7b53d74149dbb47f47fd
Created March 2, 2015 19:39
yahoo oauth1 get_request_token example
https://api.login.yahoo.com/oauth/v2/get_request_token?oauth_callback=http%3A%2F%2Flocalhost&oauth_consumer_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx--&oauth_nonce=9D342B75-2A27-4A88-93E5-53DD4AE292F8&oauth_signature=sC0HZuTJySrxLCASwkvzbOwl1J4%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1425086133&oauth_version=1.0
signature is generated from putting these parameters into the base string, in correct sorted order:
oauth_callback=http%3A%2F%2Flocalhost&
oauth_consumer_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx--&
oauth_nonce=53DBBE7A-9A1F-4113-BFD3-26B31547CEA7&
oauth_signature_method=HMAC-SHA1&
oauth_timestamp=1425324801&
oauth_version=1.0
@jpmhouston
jpmhouston / gist:680ba3a3c09039c66230
Last active August 29, 2015 14:16
successful yahoo oauth1 access, unsuccessful request
** Generating signature for https://api.login.yahoo.com/oauth/v2/get_request_token
** Parameters for base signature string:
oauth_callback=http%3A%2F%2Flocalhost&
oauth_consumer_key=dj0yJmk9Q1BTWWoxZVd0QlBGJmQ9WVdrOWVGcEVWbWxTTldrbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD1hMQ--&
oauth_nonce=48FD2DE7-0380-40AA-BF8C-3C61341ACCCE&
oauth_signature_method=HMAC-SHA1&
oauth_timestamp=1425329590&
oauth_version=1.0
** Base signature string:
@jpmhouston
jpmhouston / gist:131b96d02830d4a540ce
Created June 2, 2015 16:32
Onename vertification
Verifying I am +jpmhouston on my passcard. https://onename.com/jpmhouston
@jpmhouston
jpmhouston / gist:daa75345f6a017ce1681
Created June 19, 2015 16:18
POST with both URL parameters & JSON body in AFNetworking
@interface MixedPOSTHTTPSessionManager : AFHTTPSessionManager
- (NSURLSessionDataTask *)POST:(NSString *)URLString
parameters:(id)parameters
JSONParameters:(NSDictionary *)jsonParameters
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure;
@end
@implementation MixedPOSTHTTPSessionManager
@jpmhouston
jpmhouston / gist:35686fc811b1c62effe5
Last active August 29, 2015 14:23
Shrink UITextField placeholder to fit
// call from a view controller's -viewDidLayoutSubviews or -viewWillAppear:
// pass it a text field, etc, assign result to that text field's attributedPlaceholder property
// note that the maxKerning parameter must be a negative value (a "maximally negative" kerning value)
// pro tip: i've sometimes found that a text field still has incorrect bounds in -viewDidLayoutSubviews
// and needs to forcibly have its layout done:
//- (void)viewDidLayoutSubviews {
// [self.field layoutIfNeeded];
// self.field.attributedPlaceholder = [self resizedPlaceholderForField:self.field withPadding:4.0 maxKerningReduction:0.3];
//}
@jpmhouston
jpmhouston / PushPopNotifyingNavigationController.h
Last active December 20, 2015 10:49
PushPopNotifyingNavigationController. Add new notifications to UINavigationController, for when interested in when a view is specifically pushed or popped, instead of the normal delegate method which are called when views are revealed in either manner indistinguishably.
//
// PushPopNotifyingNavigationController.h
//
//
// Created by Pierre Houston on 2013-07-29.
//
// Add new notifications to UINavigationController, for when interested in when a
// view is specifically pushed or popped, instead of the normal delegate method
// which are called when views are revealed in either manner indistinguishably.
@jpmhouston
jpmhouston / gist:f25d560cbd771d0f74f3d7b2d0ce6fbd
Last active August 8, 2016 20:20
Swift Array element(at:), why is this missing from the standard library?
extension Array {
func element(at index: Int) -> Element? {
return index >= 0 && index < count ? self[index] : nil
}
}