Skip to content

Instantly share code, notes, and snippets.

@mdznr
mdznr / ConditionallyAssignValueUsingCPreprocessorMacros.c
Last active August 29, 2015 13:56
Wondering if there's a better way to set a value conditionally using C pre-processor macros.
// Is there a better way to do this?:
static int someIntegerValue =
#ifdef SOME_MACRO
4
#else
2
#endif
;
// Or even this?:
@mdznr
mdznr / UIViewAutoresizing Additions
Created April 20, 2014 23:54
Some quick additions to UIViewAutoresizing to make creating flexible dimensions and flexible margins easier.
#define UIViewAutoresizingFlexibleDimensions (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)
#define UIViewAutoresizingFlexibleMargins (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin)
@mdznr
mdznr / LOL
Created April 21, 2014 04:47
Please don't.
#include <stdio.h>
#define MAIN int main(int argc, const char * argv[]) { printf("Hello, World!\n"); return 0; }
MAIN
@mdznr
mdznr / Nope
Last active August 29, 2015 14:00
"Why won't my program work!?"
#include <stdio.h>
#include <stdlib.h>
#define main main(){ return EXIT_SUCCESS; } int nope
int main()
{
printf("Hello, World!\n");
return EXIT_SUCCESS;
}
@mdznr
mdznr / Program Loader
Last active August 29, 2015 14:00
Loading program...
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define main main() { printf("Loading program..."); unsigned int i=1; while (1) { sleep(i++); printf("."); fflush(stdout); } } int nope
int main()
{
printf("Hello, World!\n");
return EXIT_SUCCESS;
@mdznr
mdznr / UIColor Creation
Created May 21, 2014 19:38
Is there a speed difference between [UIColor colorWithRed:green:blue:alpha:] and [UIColor colorWithHue:saturation:brightness:alpha:]?
CGFloat red = 0.08f;
CGFloat green = 0.49f;
CGFloat blue = 0.98f;
CGFloat hue = 0.59f;
CGFloat saturation = 0.92f;
CGFloat brightness = 0.98f;
CGFloat alpha = 1.0f;
@mdznr
mdznr / UIAppearance.m
Created May 31, 2014 22:02
An example of how to customize the appearance of `UIBarButtonItem` for @manolosavi
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[[UIImage imageNamed:@"IMG"] resizableImageWithCapInsets:UIEdgeInsetsMake(4, 4, 4, 4)]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"Courier" size:16],
NSForegroundColorAttributeName: [UIColor redColor]}
forState:UIControlStateNormal];
@mdznr
mdznr / Switch
Last active August 29, 2015 14:02
The way switch statements work is different in Swift than in other languages, like C.
var total : Int
let myString = "foo"
// When originally looking at Swift's switch statements, you may think this is how you get fall through behaviour:
total = 0
switch myString {
case "foo":
total += 4
case "foo", "bar":
total += 2
@mdznr
mdznr / Alignment
Last active August 29, 2015 14:02
How do you best align function calls on multiple lines?
// In Objective-C, a relatively long method call could be easily wrapped on multiple lines:
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, attributedString.length)];
// But how do you do that in Swift?
attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
@mdznr
mdznr / defaults delete regular expression
Created August 25, 2020 19:03
Easily delete all user defaults matching a given format
defaults read <domain> | grep -o --regexp="\"NSWindow Frame [^\"]*\"" | xargs -L1 defaults delete <domain>