Skip to content

Instantly share code, notes, and snippets.

void objc_setAssociatedObject ( id object, const void *key, id value, objc_AssociationPolicy policy );
// object = usually 'self'. This is the object you want to associate a key and value with.
// key = the key you want to associate the object with
// value = the value you want to associate with the key
// objc_AssociationPolicy is one of the below:
enum {
OBJC_ASSOCIATION_ASSIGN = 0,
OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1,
---
Language: Cpp
BasedOnStyle: WebKit
# AccessModifierOffset: -4
# ConstructorInitializerIndentWidth: 4
# AlignEscapedNewlinesLeft: false
# AlignTrailingComments: false
# AllowAllParametersOfDeclarationOnNextLine: true
# AllowShortBlocksOnASingleLine: false
# AllowShortCaseLabelsOnASingleLine: false
@vinnybad
vinnybad / EVCSomeClass.m
Last active August 29, 2015 14:15
Unsafe Singleton Using @synchronized
@implementation EVCSomeClass
+(EVCSomeClass *)shared {
static EVCSomeClass *shared = nil;
if( !shared ) {
shared = [EVCSomeClass new];
}
return shared;
@vinnybad
vinnybad / EVCSomeClass.m
Last active August 29, 2015 14:15
Thread-safe Singleton using @synchronized
@implementation EVCSomeClass
+(EVCSomeClass *)shared {
static EVCSomeClass *shared = nil;
// synchronizes by locking on the Class EVCSomeClass
@synchronized([EVCSomeClass class]) {
shared = [EVCSomeClass new];
}
@vinnybad
vinnybad / EVCSomeClass.m
Last active August 29, 2015 14:15
Thread-safe Singletons in Modern Objective-C
@implementation EVCSomeClass
+(EVCSomeClass *)shared {
static EVCSomeClass *shared = nil;
// dispatch_once_t is typedef'd to a long
static dispatch_once_t pred;
// this block is executed synchronously, and only one time ever for the given predicate
dispatch_once(&pred, ^{
@vinnybad
vinnybad / EVCCommon.h
Created February 13, 2015 06:13
Thread-safe Singletons in Modern Objective-C (The Short Way)
// the block that's passed in needs to return an object
#define DEFINE_SHARED_INSTANCE_USING_BLOCK(block) \
static dispatch_once_t pred = 0; \
__strong static id _sharedObject = nil; \
dispatch_once(&pred, ^{ _sharedObject = block(); }); \
return _sharedObject;
@vinnybad
vinnybad / BJMSearchBar.h
Created September 9, 2015 04:51
UISearchController in iOS 8 - Working around showing multiple 'Cancel' button quirks
//
// BJMSearchBar.h
//
// Created by Vinayak Ram on 9/8/15.
//
#import <UIKit/UIKit.h>
@interface BJMSearchBar : UISearchBar
@vinnybad
vinnybad / findAndZipFiles.sh
Created October 1, 2015 15:51
Finding all files of a particular type (e.g. all .strings files for iOS internalization) and adding them to a zip
#!/bin/bash
find . -name '*.strings' -print | egrep -v 'Pods' | zip source -@
# Break it down:
#
# Find all files with file name ending with .strings
# find . -name '*.strings' -print
#
# Search for all lines that don't have 'Pods' in it (the -v means invert)
@vinnybad
vinnybad / BJMWebService.m
Last active October 3, 2015 20:26
Download a file using AFNetworking
/*
* Downloading files using AFNetworking is not a 1-step process, but it's still pretty easy.
*
* NOTE: If you're downloading a file from S3, make sure to set the proper Content-Type. For example,
* a zip file should ideally have the content type of application/zip.
*
* Inspired by various sources online.
*/
#pragma mark - Downloading Files
@vinnybad
vinnybad / genAllStrings.m
Last active November 6, 2015 03:09
Generate .strings files for a particular language (overwrites existing strings file)
#!/bin/bash
# find all .m files and pump it through genstrings, outputting it to en.lproj/Localizable.strings
find . -name '*.m' | xargs genstrings -o en.lproj