Skip to content

Instantly share code, notes, and snippets.

View neilkimmett's full-sized avatar

Neil Kimmett neilkimmett

View GitHub Profile
struct APIError: NetworkError {
let json: JSON
let apiCode: Int?
let message: String?
let httpResponse: HTTPURLResponse
@khanlou
khanlou / Enum+CaseCountable.swift
Created November 15, 2016 18:19
count and allValues on Int-based enums
protocol CaseCountable { }
extension CaseCountable where Self : RawRepresentable, Self.RawValue == Int {
static var count: Int {
var count = 0
while let _ = Self(rawValue: count) { count+=1 }
return count
}
@KingOfBrian
KingOfBrian / PublicationCenter.swift
Last active February 4, 2022 11:49
PublicationCenter
//
// PublicationCenter.swift
//
// Created by Brian King on 5/17/16.
// Copyright © 2016 Raizlabs. All rights reserved.
//
import Foundation
/// PublicationCenter is an NSNotificationCenter style object that
@irace
irace / CenteringView.swift
Last active November 29, 2017 19:31
I’m building a complex new app entirely with programmatic Auto Layout. It only supports iOS 9 so that means `UIStackView` and `NSLayoutAnchor` exclusively. These two classes have been very handy thus far, in the spirit of composition over inheritance.
final class CenteringView: UIView {
// MARK: - Initialization
init(contentView: UIView) {
super.init(frame: .zero)
addSubview(contentView)
contentView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activateConstraints([
@pietbrauer
pietbrauer / Rakefile
Created April 14, 2015 12:02
Rake task for cleaning header comments from objc files
namespace :housekeeping do
desc "Remove all header-comments"
task :remove_header_comments do
["Classes", "Tests"].each do |directory|
files = Dir["./#{directory}/**/*"].select { |value| File.file?(value) }
files.each do |file|
lines_array = File.open(file).readlines
if lines_array.first.match(/^\/\//) && !lines_array.first.match(/^\/\/ DO NOT EDIT/)
text = lines_array.drop_while { |line| line.match(/^\/\/|^\n/) }
File.open(file, 'w') do |f|
@Sephiroth87
Sephiroth87 / ⦅╯°□°⦆╯
Created February 17, 2015 17:42
Swift flipping operator ⦅╯°□°⦆╯
let conversionMap: [Character: Character] = [
"\u{0021}" : "\u{00A1}",
"\u{0022}" : "\u{201E}",
"\u{0026}" : "\u{214B}",
"\u{0027}" : "\u{002C}",
"\u{0028}" : "\u{0029}",
"\u{002E}" : "\u{02D9}",
"\u{0033}" : "\u{0190}",
"\u{0034}" : "\u{152D}",
"\u{0036}" : "\u{0039}",
@steipete
steipete / Macros.h
Last active January 6, 2024 07:24
Declare on your main init that all other init methods should call. It's a nice additional semantic warning. Works with Xcode 5.1 and above. Not tested with earlier variants, but should just be ignored. A reference to this macro shortly appeared in https://developer.apple.com/library/ios/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObj…
#ifndef NS_DESIGNATED_INITIALIZER
#if __has_attribute(objc_designated_initializer)
#define NS_DESIGNATED_INITIALIZER __attribute((objc_designated_initializer))
#else
#define NS_DESIGNATED_INITIALIZER
#endif
#endif
/**
Provides the ability to verify key paths at compile time.
If "keyPath" does not exist, a compile-time error will be generated.
Example:
// Verifies "isFinished" exists on "operation".
NSString *key = SQKeyPath(operation, isFinished);
// Verifies "isFinished" exists on self.
@simonwhitaker
simonwhitaker / swmath.m
Last active January 1, 2016 22:58
Architecture-independent floor() function
/*
* Calling math.h functions like `floor` and `floorf` on CGFloat variables
* becomes problematic when compiling the same code for both 32-bit and
* 64-bit platforms, since CGFloat is double on 64-bit, float on 32-bit.
* Hence, if you compile with -Wconversion set, `floorf` will give a warning
* on 64-bit, while `floor` gives a warning on 32-bit.
*
* Here's a suggested implementation of an architecture-independent `floor`
* function, written using plain old function overloading which is enabled
* using the `__attribute__((overloadable))` Clang language extension.
@tomlea
tomlea / hero.sh
Created August 1, 2013 16:02
When you have multiple heroku apps, for one repo, you're going to need a hero! hero production run rake db:migrate
function _hero_getRemoteNames(){
git config --get-regexp remote.*.url heroku.com |
sed -E 's/^remote\.([^\.]*)\.url .*$/\1/'
}
function _hero_herokuAppFor(){
git config --get remote.$1.url |
sed -E 's/^git@heroku.com:([^.]*)\.git/\1/'
}