Skip to content

Instantly share code, notes, and snippets.

View paulrehkugler's full-sized avatar

Paul Rehkugler paulrehkugler

View GitHub Profile
@paulrehkugler
paulrehkugler / Constants.swift
Created March 29, 2016 19:03
Force Initialization for Constants
struct Constants {
let leftPadding: CGFloat
let rightPadding: CGFloat
static func defaultConfiguration() -> Constants { /* init */ }
static func compactConfiguration() -> Constants { /* init with different stuff */ }
}
@paulrehkugler
paulrehkugler / TMObjectThatHasLotsOfDependenciesInjected.h
Last active December 1, 2015 17:25
Nullability and Inheritance
@interface TMObjectThatHasLotsOfDependenciesInjected : NSObject
- (nonnull instancetype)initWithDependency:(TMDependency * __nonnull)dependency
anotherDependency:(TMOtherDependency * __nonnull)anotherDependency NS_DESIGNATED_INITIALIZER;
/// option 1: break inheritance
- (nullable instancetype)init __attribute__((unavailable("Use initWithDependency:anotherDependency: instead")));
@end
@paulrehkugler
paulrehkugler / RecursiveBlock.m
Last active November 7, 2024 18:16
Recursive Block
// Follow up to - https://gist.github.com/paulrehkugler/11063725
BOOL baseCaseCondition = NO; // obviously this should be data driven, not hardcoded
typedef void (^RecursiveBlock)(void (^)());
RecursiveBlock aRecursiveBlock;
aRecursiveBlock = ^(RecursiveBlock block){
if ((baseCaseCondition) && block)
{
@paulrehkugler
paulrehkugler / Nope.m
Created April 18, 2014 20:48
Block Pointer - Doesn't work
// trying to make a block pointer
typedef void (^DoingBadStuffWithBlocks)();
DoingBadStuffWithBlocks *theCompilerHatesThis = &^{
if (false)
{
*theCompilerHatesThis();
}
}();
@interface UINavigationController(Completion)
- (void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void (^)(void))completion;
@end
@paulrehkugler
paulrehkugler / gist:9647085
Created March 19, 2014 17:37
Default Protocol Method Implementation in Objective-C
// SomeProtocol.h
@protocol SomeProtocol <NSObject>
- (void) protocolMethod;
@end
// NSObject+SomeProtocolDefaultImplementation.h
@interface NSObject(SomeProtocolDefaultImplementation)
@paulrehkugler
paulrehkugler / MacroFailure.m
Created March 12, 2014 18:37
Macro fails when NSArray literals (i.e. @[]) aren't wrapped in parentheses (i.e. (@[])).
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
#define IOS7_CODE_WITH_FALLBACK(iOS7_code, other_code) \
do { \
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {\
iOS7_code \
}\
else { \
other_code \
} \
//
// NSObject+PRKeyValueObserving.h
// PRKeyValueObserving
//
// Created by Paul Rehkugler on 3/8/14.
// Copyright (c) 2014 Paul Rehkugler. All rights reserved.
//
#import <Foundation/Foundation.h>
@paulrehkugler
paulrehkugler / gist:9360998
Last active August 29, 2015 13:57
Proper string format tokens for NSInteger, NSUInteger, and CGFloat... Ewww
- (BRYDescriptionBuilder *)appendInteger:(NSInteger)integer withName:(NSString *)name {
#if defined(__LP64__) && __LP64__
return [self appendString:[NSString stringWithFormat:@"%li", integer] withName:name];
#else
return [self appendString:[NSString stringWithFormat:@"%i", integer] withName:name];
#endif
}
- (BRYDescriptionBuilder *)appendUnsignedInteger:(NSUInteger)unsignedInteger withName:(NSString *)name {
#if defined(__LP64__) && __LP64__
/* MyModelObject.h */
@interface MyModelObject : NSObject
// this data is "publicly" immutable
@property (nonatomic, readonly) NSArray *someData;
@end