Skip to content

Instantly share code, notes, and snippets.

Avatar

James Frost frosty

View GitHub Profile
View gist:8a163db3f7f3d81d5296
Your goals are to reduce the number of things that you have to keep in your head at any given moment, and to rely as little as possible on your own ability to consistently do things right.
If you make a thing immutable ('let' in swift), you never have to think about what happens if it changes, or what other parts of the code you'll effect if you change it.
If you split complex functions into several smaller functions that only interact by passing arguments or getting return values, then you limit the amount of code you need to consider when hunting for a bug, and you can test each small piece separately.
If you understand what things must be true in your code (aka invariants, for example "a person's age must be greater than 0"), and either provide no function that can cause them to be untrue, or check and crash immediately when they're untrue, then you don't have to debug issues caused by incorrect assumptions.
If you remove possibilities (for example, Swift removes the possibility of things being nil unless
@mattt
mattt / nshipster-new-years-2015.md
Created Nov 25, 2014
NSHipster New Year's 2015
View nshipster-new-years-2015.md

Season's Greetings, NSHipsters!

As the year winds down, and we take a moment to reflect on our experiences over the past months, one thing is clear: 2014 has been an incredible year professionally for Apple developers. So much has happened in such a short timespan, and yet it's hard to remember our relationship to Objective-C before Swift, or what APIs could have captivated our imagination as much as iOS 8 or WatchKit.

It's an NSHipster tradition to ask you, dear readers, to send in your favorite tips and tricks from the past year for publication over the New Year's holiday. This year, with the deluge of new developments—both from Cupertino and the community at large—there should be no shortage of interesting tidbits to share.

Submit your favorite piece of Swift or Objective-C trivia, framework arcana, hidden Xcode feature, or anything else you think is cool, and you could have it featured in the year-end blowout article. Just comment on this gist below!

If you're wondering about what to post, look to

@marcoarment
marcoarment / gist:1105553afba6b4900c10
Created Oct 21, 2014
Variable-height UITableView tableHeaderView with autolayout
View gist:1105553afba6b4900c10
// in a UITableViewController (or any other view controller with a UITableView)
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, 0)];
header.translatesAutoresizingMaskIntoConstraints = NO;
// [add subviews and their constraints to header]
NSLayoutConstraint *headerWidthConstraint = [NSLayoutConstraint
View DispatchOperators.swift
// Dispatch
//
// Usage:
// Async dispatch to main queue
// -->block
// Async dispatch to queue
// queue --> block
// Sync dispatch to main queue
// -->|block
// Sync dispatch to queue
@benrigas
benrigas / Swift JSON Playground
Last active Nov 2, 2017
Swift Playground for initializing objects from Dictionary/JSON
View Swift JSON Playground
// Playground - noun: a place where people can play
import Cocoa
extension String {
func dictionaryFromJSON () -> Dictionary<String, AnyObject>? {
if let dict = NSJSONSerialization.JSONObjectWithData(self.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true),
options: .AllowFragments,
error: nil) as? Dictionary<String,AnyObject>
{
View ArrayHelpers.swift
extension Array {
func first() -> Element? {
if isEmpty {
return nil
}
return self[0]
}
func last() -> Element? {
View NSManagedObjectContext+DebugSwizzling.h
#if DEBUG
/**
Toggles assertion of Core Data managed object contexts. Only available when DEBUG is defined.
When enabled, your application will throw an exception when accessing or modifying entities in a context other than their own.
*/
void TCBToggleAssertingContextQueues();
#endif
@jrturton
jrturton / gist:6364558
Last active Dec 21, 2015
Navigation back buttons themed like iOS7, for iOS 5 and 6
View gist:6364558
-(void)themeBackButtons
{
// Appearance proxy methods
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage backButtonImage] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(7.0, 0.0) forBarMetrics:UIBarMetricsDefault];
}
// The image above is generated by this method
+(UIImage *)backButtonImage
{
@supermarin
supermarin / gist:5597771
Last active Dec 17, 2015
Testing asynchronous methods with Kiwi
View gist:5597771
//
// BeerTests.m
// Beer
//
// Created by Marin Usalj on 5/16/13.
// Copyright 2013 @mneorr | mneorr.com. All rights reserved.
//
#import "Kiwi.h"
@jrturton
jrturton / gist:4690976
Created Feb 1, 2013
switch out a screenful of content for another one, in a left or right direction.
View gist:4690976
-(void)changeArticle:(UIButton*)sender
{
CGFloat multiplier = (sender == self.nextButton) ? 1.0 : -1.0;
// Render to an image and drop in the new version
UIGraphicsBeginImageContextWithOptions(self.articleContainer.frame.size, YES, 0);
CGContextRef c = UIGraphicsGetCurrentContext();
[self.articleContainer.layer renderInContext:c];
UIImage *articleScreenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
You can’t perform that action at this time.