Skip to content

Instantly share code, notes, and snippets.

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 November 25, 2014 19:38
NSHipster New Year's 2015

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

// 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 November 2, 2017 14:51
Swift Playground for initializing objects from Dictionary/JSON
// 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>
{
@calebd
calebd / ArrayHelpers.swift
Last active November 4, 2022 15:17
Swift Helpers
extension Array {
func first() -> Element? {
if isEmpty {
return nil
}
return self[0]
}
func last() -> Element? {
#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 December 21, 2015 20:58
Navigation back buttons themed like iOS7, for iOS 5 and 6
-(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 December 17, 2015 10:49
Testing asynchronous methods with Kiwi
//
// 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 February 1, 2013 12:16
switch out a screenful of content for another one, in a left or right direction.
-(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();
@kvnsmth
kvnsmth / example-subtree-usage.md
Last active March 5, 2023 21:58
A real world usage for git subtrees.

Let's say you have an iOS project, and you want to use some external library, like AFNetworking. How do you integrate it?

With submodules

Add the project to your repo:

git submodule add git@github.com:AFNetworking/AFNetworking.git Vendor/AFNetworking

or something to that effect.