Skip to content

Instantly share code, notes, and snippets.

@Air-Craft
Air-Craft / add_to_collection_preserving_order.m
Created March 14, 2015 20:39
Utility for inserting new entries in set X preserving order defined in set M #objective-c #cocoa #collections #sorting #intermediate
/** Utility for inserting new entries in set X preserving order defined in set M */
- (void)_addEntry:(id)entryToInsert toSet:(NSMutableOrderedSet *)mutatingSet respectingOrderIn:(NSMutableOrderedSet *)masterSet
{
NSInteger entryIdxInMaster = [masterSet indexOfObject:entryToInsert];
NSAssert(entryIdxInMaster != NSNotFound, @"The setWithOrder must contain the entryToInsert");
// Loop through the set and find the index of the place to insert the new entry
__block NSInteger idxToInsertBefore = NSNotFound;
[mutatingSet enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
@Air-Craft
Air-Craft / NSDateFormatter_format_examples.txt
Created March 4, 2015 17:14
NSDateFormatter format examples #basic #cocoa #datetime
"yyyy-MM-d H:m:s" // 2015-03-12 16:01:22
"EEE d LLL" // Thu 3 Feb
@Air-Craft
Air-Craft / nsdateformatter_format_ref.txt
Created March 4, 2015 16:32
NSDateFormatter format reference #cocoa #basic #reference #datetime
a: AM/PM
A: 0~86399999 (Millisecond of Day)
c/cc: 1~7 (Day of Week)
ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat
cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday
d: 1~31 (0 padded Day of Month)
D: 1~366 (0 padded Day of Year)
@Air-Craft
Air-Craft / parse_ios_datetime.m
Last active August 29, 2015 14:16
Parse a standard date/time string in iOS #cocoa #basic #datetime
NSDateFormatter *dateParser = [[NSDateFormatter alloc] init];
dateParser.dateFormat = @"yyyy-MM-d H:m:s";
return [dateParser dateFromString:self.estimatedArrivalDateStr];
@Air-Craft
Air-Craft / custom_fullscreen_modals.m
Created February 27, 2015 14:23
Present/dismiss a custom full screen modal alert #modals #ios #views #intermediate
static const CGFloat _BG_ALPHA = 0.3;
static const NSTimeInterval _ANIM_TIME = 0.3;
static UIWindow *_mainWindow;
static UIWindow *_alertWindow;
- (void)showExplainerScreen
{
_mainWindow = [[UIApplication sharedApplication] keyWindow];
@Air-Craft
Air-Craft / uiview_rounded_rect_shadow.m
Last active August 29, 2015 14:16
Correct shadow on a UIView with rounded rect / corner radius #objective-c #ios #styles #basic
_containerView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:_containerView.bounds cornerRadius:_containerView.layer.cornerRadius].CGPath;
@Air-Craft
Air-Craft / uinavvc_push_with_callback.m
Created February 11, 2015 16:42
Push a VC onto UINavigationController with completion callback #ios #transitions #intermediate
- (void)pushViewController:(UIViewController *)viewController
animated:(BOOL)animated
completion:(void (^)(BOOL finished))completion
{
[self pushViewController:viewController animated:animated];
if (animated) {
[self.transitionCoordinator
animateAlongsideTransition:nil
completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
@Air-Craft
Air-Craft / http_headers_from_sessiondatatask.m
Created February 10, 2015 13:24
Get the HTTP Headers from an NSURLSessionDataTask #ios #networking #API #basic
NSHTTPURLResponse *response = ((NSHTTPURLResponse *)[task response]);
NSDictionary *headers = [response allHeaderFields];
@Air-Craft
Air-Craft / cocoa_format_currency.m
Created February 6, 2015 16:36
Format currency in Cocoa using the locale #cocoa #formatting #currency #i8n
NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
fmt.numberStyle = NSNumberFormatterCurrencyStyle;
// fmt.alwaysShowsDecimalSeparator = YES;
// fmt.allowsFloats = YES;
// fmt.maximumFractionDigits = 2;
// fmt.locale = [NSLocale currentLocale]; Fixed for now
fmt.locale = [NSLocale localeWithLocaleIdentifier:@"en_GB"];
_priceLbl.text = [fmt stringFromNumber:@(totalPrice)];
@Air-Craft
Air-Craft / cocoa_date_formatting.m
Created February 5, 2015 14:36
#basic #objective-c #formatting #datetime #cocoa
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"EEE d LLL"; // Thu 3 Feb
_estimatedArrivalDate.text = [dateFormatter stringFromDate:estimatedDate];