Skip to content

Instantly share code, notes, and snippets.

View IanKeen's full-sized avatar
🏂

Ian Keen IanKeen

🏂
View GitHub Profile
// Overriding NSLocalizedString to use NSLocalizedStringFromTableInBundle instead
#define currentLanguageBundle [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[[NSLocale preferredLanguages] objectAtIndex:0] ofType:@"lproj"]]
#ifdef NSLocalizedString
#undef NSLocalizedString
#endif
#define NSLocalizedString(key, comment) NSLocalizedStringFromTableInBundle(key, nil, currentLanguageBundle, comment)
@quinntaylor
quinntaylor / CompilerCheckedKeyPaths.h
Created November 13, 2014 06:14
Code for getting compiler check on key paths, more accurate than using NSStringFromSelector(@selector(foo)).
// See http://nshipster.com/key-value-observing/
// Put this code a common utilities header, and use it to have the compiler help check correctness of key paths.
// Uses macro stringification to create an Obj-C string literal, plus validation code that the compiler optimizes out.
@interface NSObject (KeyPathFakeCategoryForCompilerWarnings)
+ (instancetype)__fake_method_for_compiler_warnings__;
- (instancetype)__fake_method_for_compiler_warnings__;
@end
/*! Returns a string for the given keypath, but causes a compiler warning if the keypath is not defined on \c self.
@natecook1000
natecook1000 / DictionaryObjectsForKeys.swift
Last active December 9, 2015 18:43
Initializers for Dictionary like NSDictionary +dictionaryWithObjects:forKeys:
// (c) 2015 Nate Cook, licensed under the MIT license
//
// Initializers for Dictionary like NSDictionary +dictionaryWithObjects:forKeys:
public extension Dictionary {
/// Creates a new Dictionary from the given `keys` and `values` collections.
///
/// More efficient than the sequence version because it can reserve the correct
/// capacity before filling the dictionary. Returns `nil` if the lengths of the
/// two collections differ.
@nicklockwood
nicklockwood / gist:7446228
Last active December 28, 2015 04:59
Why having a single success/error callback block is better than having separate ones

Suppose you have a network method that only uses a single block callback

typedef void (^Handler)(BOOL success, id response, NSError *error);

- (void)makeRequestWithHandler:(Handler)handler;

But you have to make this request dozens of times, and you want to reuse the same failure handler in most cases? Easy, just have a factory function that returns a block:

typedef void (^SuccessHandler)(id response);

typedef void (^ErrorHandler)(NSError *error);

@daehn
daehn / CountElement.Swift
Last active January 30, 2016 19:50
Extension on SequenceType to count the occurrence of an element.
extension SequenceType where Generator.Element : Equatable {
func count(other: Generator.Element) -> Int {
var sum = 0
for case other in self { sum++ }
return sum
}
}
func ~=<T>(pattern: T -> Bool, value: T) -> Bool {
return pattern(value)
@terhechte
terhechte / slow_kvo_dictionary_example2.m
Created December 7, 2011 20:34
Example 1 of slow and fast NSDictionary access
// Created by Benedikt Terhechte on 07.12.11.
// Appventure.me
//
#import <Foundation/Foundation.h>
#define AKeyPathDictionary(dictionary, A) [dictionary objectForKey:@A]
#define BKeyPathDictionary(dictionary, A, B) [AKeyPathDictionary(dictionary, A) objectForKey:@B]
#define CKeyPathDictionary(dictionary, A, B, C) [BKeyPathDictionary(dictionary, A, B) objectForKey:@C]
#define DKeyPathDictionary(dictionary, A, B, C, D) [CKeyPathDictionary(dictionary, A, B, C) objectForKey:@D]
@loganwright
loganwright / EnumExtensions.swift
Last active July 15, 2016 18:31
Int enum type extensions
protocol RawIntInitable {
init?(rawValue: Int)
}
extension RawIntInitable {
static var allCases: [Self] {
var caseIndex = 0
let generator = anyGenerator { Self(rawValue: caseIndex++) }
return Array(generator)
#pragma mark - Delintage
- (void) lint: (NSString *) path
{
NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
if (!string) return;
NSArray *lines = [string componentsSeparatedByString:@"\n"];
int count = 0;
int cautions = 0;
@nicklockwood
nicklockwood / gist:7447381
Last active February 14, 2017 09:31
Why I still prefer nibs to storyboards.

Storyboard Segues initially seem like a pretty cool way to construct interfaces using minimal glue code. But actually, ordinary nibs already support this, and in a much more flexible way.

Certainly, a Storyboard lets you bind a button action up to display a view controller with no code, but in practice you will usually want to pass some data to the new controller, depending on which button you used to get there, and this means implementing the -prepareForSegue:sender: method, which rapidly becomes a giant if/elseif statement of doom, negating most of the benefit of the codeless segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"modalSegue"])
    {
        ModalViewController *controller = (ModalViewController *)segue.destination;

controller.someProperty = someValue;

@bwhiteley
bwhiteley / Nil.swift
Last active March 2, 2017 08:31
NilLiteralConvertible can lead to surprising behavior
struct Foo : CustomStringConvertible {
let text:String
init(text:String) {
self.text = text
}
var description:String { return self.text }
}