|
CoffeeScript
Override a Backbone.js Model’s Constructor in CoffeeScript »
|
class BehindTheBarApp.Models.Order extends Backbone.Model
constructor: (@orders) ->
super *arguments |
|
Objective-C
Sort an NSArray of Nested Dictionaries via a Key’s Value »
|
NSArray *arr = @[
@{@"label": @"one", @"count": @{@"k": @5} },
@{@"label": @"two", @"count": @{@"k": @9} },
@{@"label": @"three", @"count": @{@"k": @1} },
@{@"label": @"four", @"count": @{@"k": @100} },
];
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"count.k" ascending:NO];
NSArray *arr2 = [arr sortedArrayUsingDescriptors:@[sd]]; |
|
C
Doxygen Member Grouping vs Comment Sharing »
|
// MEMBER GROUPING
// Creates a headed section with the detailed description underneath the header
/** @name Channel-channel math ops.
Channel-channel math ops with a bit more flexibility than the operator overloads; exception on bounds error if debug is set.
\param ch Source channel
\return Reference to self for chaining
*/
//@{
...
//@}
// COMMENT SHARING
// No header but share the comment amongst all enclosed members. Must set DISTRIBUTE_GROUP_DOC = YES in config
//@{
/** Brief description
etc...
*/
...
//@} |
|
iOS
Draw a Shadow Under a UIView »
Credits:
|
self.layer.masksToBounds = NO;
self.layer.cornerRadius = 8; // optional
self.layer.shadowOffset = CGSizeMake(10, 10);
self.layer.shadowRadius = 5;
self.layer.shadowOpacity = 0.5; |
|
iOS
Vibrate / Buzz the iPhone Even When Not in Silent Mode »
Seem like iPhone5+ might have changed the ability...
|
// Allocate system sound for buzz
SystemSoundID _soundID;
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"silence" ofType:@"wav"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath: soundPath]), &_soundID);
// later...
AudioServicesPlayAlertSound(_soundID); // ...PlayAlertSound, not ...PlaySystemSound |
|
iOS
Get iPhone/iPad Screen Dimensions »
|
CGSize screenSize = UIScreen.mainScreen.applicationFrame.size; |
|
iOS
Convert a Float to a Price in a Specific Locale with NSNumberFormatter »
|
float newPrice = 1234.56;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-GB"];
myLabel.text = [formatter stringFromNumber:@(newPrice)]; |
|
Objective-C
Objective-C Block Definition Syntax Master Cheatsheet »
A very good reference:
|
//
// TYPEDEFS
//
typedef int (^MyBlockTypeWithArgs)(int, int);
typedef int (^MySimpleBlockType)();
// Obj-C object argument/return types are ok too of course...
//
// LOCAL VARS
//
int (^multiply)(int, int) = ^int(int a, int b) { return a*b; };
int (^multiply)(int, int) = ^(int a, int b) { return a*b; }; // same as above
MyBlockTypeWithArgs multiple = ^(int a, int b) { return a*b; };
MySimpleBlockType doSomething = ^{ [self _doSomething]; };
//
// PROPERTIES (same as local vars)
//
@property (nonatomic, copy) int (^multiply)(int, int);
@property (nonatomic, strong) MyBlockTypeWithArgs callbackBlock;
//
// METHOD ARGUMENTS
//
@interface MyClass
- (void)doItWithBlock:(int(^)(int, int))blockTakingArgs;
- (void)doItWithBlock3:(void (^)())simpleBlock;
- (void)doItWithBlock:(MyBlockTypeWithArgs)block;
@end |
|
iOS
Play a Short Sound / Audio Clip on iOS – 2 Ways »
|
// Cocoa-style: AVAudioPlayer
#import <AVFoundation/AVFoundation.h>
clickSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSBundle.mainBundle URLForResource:@"click" withExtension:@"mp3"] error:NULL];
clickSound.volume = 0.5;
[clickSound playAtTime:0]; // OR...
clickSound.currentTime = 0;
[clickSound play];
// Cocoa-style: AVPlayer
#import <AVFoundation/AVFoundation.h>
clickSound = [AVPlayer playerWithURL:[NSBundle.mainBundle URLForResource:@"click" withExtension:@"mp3"]];
// Note, no volume on this :(
clickSound seekToTime:CMTimeMake(0,1)]; // rewind if play occurs repeatedly and loading is only on init
[clickSound play];
// Core Foundation style
#import <AudioToolbox/AudioToolbox.h>
AudioServicesCreateSystemSoundID(.... URL ...., &mySound);
AudioServicesPlaySystemSound(mySound);
AudioServicesDisposeSystemSoundID(mySound); |
|
C++
C++ Operator Overloading Boilerplate »
|
// Arithmetic
R T::operator +(S b);
R T::operator -(S b);
R T::operator +();
R T::operator -();
R T::operator *(S b);
R T::operator /(S b);
R T::operator %(S b);
R T::operator +=(S b);
R T::operator -=(S b);
R T::operator *=(S b);
R T::operator /=(S b);
R T::operator %=(S b);
// Note: C++ uses the unnamed dummy-parameter int to differentiate between prefix and suffix increment operators.
R T::operator ++();
R T::operator ++(int);
R T::operator --();
R T::operator --(int);
// Comparison
R T::operator ==(S b);
R T::operator !=(S b);
R T::operator >(S b);
R T::operator <(S b);
R T::operator >=(S b);
R T::operator <=(S b);
// Member Access and Functor-behavior
R T::operator [](S b);
T::operator R();
R T::operator *();
R T::operator ->();
R T::operator ->*(S a);
R T::operator ()(S a1, U a2, ...);
R T::operator ,(S b);
// Assignment, reference & copy
R T::operator &();
R T::operator =(S b);
// (De)Allocation
void* T::operator new(size_t x);
void* T::operator new[](size_t x);
void T::operator delete(void* x);
void T::operator delete[](void* x);
// Logic
R T::operator !();
R T::operator &&(S b);
R T::operator ||(S b);
// Bitwise
R T::operator ~();
R T::operator &(S b);
R T::operator |(S b);
R T::operator ^(S b);
R T::operator <<(S b);
R T::operator >>(S b);
R T::operator &=(S b);
R T::operator |=(S b);
R T::operator ^=(S b);
R T::operator <<=(S b);
R T::operator >>=(S b); |
|
iOS
Rotate a UIView (2D) »
|
view.transform = CGAffineTransformMakeRotation(angleClockwise); |
|
Git
Show the Date When a File Was First Added to a Git Repo »
|
git log --format=%aD --follow -- <FILE> | tail -1 |
|
bash
Piping Two Inputs into a Linux Shell Command »
|
diff <(git log --oneline origin) <(git log --oneline) |
|
C++
Checking Whether a Key Exists in a C++ unordered_map »
|
// mymap is a std::unordered_map
if (myMap.find(key) != map.end()) {
return;
} |
|
C++
Initialise a C++ Queue with a Set of Values »
|
int poolSize = 100;
float initial = 0.0;
std::queue<float> myQueue(std::list<float>(initial, poolSize)); |
|
iOS
AFConvert a Batch of Audio Files Replacing Originals in Shell »
|
# WAVE 16bit Stereo LEI...Careful!
ls | xargs -I {} afconvert -d LEI16@44100 -c 2 -f 'WAVE' {} -o {} |
|
Python
Pythonic Way to Check For Any Overlapping Values in Two Lists »
|
main_list = [1, 2, 3]
test_a = [1, 8, 9]
test_b = [7, 8, 9]
any(item in main_list for item in test_a) # True, number 1 matches
any(item in main_list for item in test_b) # False, no matches
# The first "in" is a boolean conditional.
# The second "in" creates a generator
|
|
C++
Iterating Through Map’s, Vectors, and Set’s in C++11 »
|
// SETS & VECTORS
//
std::unordered_set<T> mySet;
for (const auto& elem: mySet) {
// ...
}
// The older way...
std::unordered_set<T> mySet;
for (auto itr = mySet.begin(); itr != mySet.end(); ++itr) {
// Use (*itr)
}
//
// MAPS
//
std::unordered_map<int, string> myMap;
for (const auto& item: myMap) {
// Use item->first, item->second
}
// Or
std::unordered_map<int, string> myMap;
for (auto itr = myMap.begin(); itr != myMap.end(); ++itr) {
// Use (*itr)->first, (*itr)->second
} |
|
iOS
Manually Load a ViewController With a Custom NIB at App Launch in iOS »
|
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// For those times when you decide to revert back from the Storyboard...
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.mainViewController = [MyCustomViewController alloc] inintWithNibName:@"MyCustomViewNib" bundle:nil];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
} |
|
Git
Clear the Index of Stage Files »
|
git reset HEAD |
|
Python
Swap Dictionary Keys/Values in Python »
|
# Method 1
dict((value, key) for key, value in my_dict.iteritems())
# Method 2 (less efficient)
new_dict = dict (zip(my_dict.values(),my_dict.keys())) |