Skip to content

Instantly share code, notes, and snippets.

@simme
simme / debounce-throttle.swift
Created December 20, 2016 10:04
Swift 3 debounce & throttle
//
// debounce-throttle.swift
//
// Created by Simon Ljungberg on 19/12/16.
// License: MIT
//
import Foundation
extension TimeInterval {
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
class LastRowCenteredLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let originalAttributes = super.layoutAttributesForElements(in: rect) else { return nil }
guard originalAttributes.isEmpty == false else { return originalAttributes }
@wojteklu
wojteklu / clean_code.md
Last active June 1, 2024 12:45
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules

@samsonjs
samsonjs / pre-commit.sh
Last active November 5, 2021 11:50
[iOS] git pre-commit hook to catch misplaced views in xibs and storyboards, and focused or disabled tests and specs
#!/usr/bin/env bash
#
# Based on http://merowing.info/2016/08/setting-up-pre-commit-hook-for-ios/
set -eu
diff-index() {
git diff-index -p -M --cached HEAD -- "$@"
}
import UIKit
// This class allows the "presentedController" to receive touches
// https://pspdfkit.com/blog/2015/presentation-controllers/
class PSPDFTouchForwardingView: UIView {
var passthroughViews: [UIView] = []
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
guard let hitView = super.hitTest(point, withEvent: event) else { return nil }
@stansidel
stansidel / NSData+Hashes.swift
Last active March 22, 2022 14:51
Getting MD5 and SHA256 of NSData and String in Swift
extension NSData {
private func getHash(
algorithm: (UnsafePointer<Void>, CC_LONG, UnsafeMutablePointer<UInt8>) -> UnsafeMutablePointer<UInt8>,
digestLength: Int32
) -> String {
let digestLen = Int(digestLength)
let fileLen = CUnsignedInt(self.length)
let buffer = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLen)
algorithm(self.bytes, fileLen, buffer)
@preble
preble / Completion.swift
Created January 20, 2016 23:32
A cancellable completion for Swift.
final class Completion<R> {
private let closure: (R) -> Void
private var cancelled = false
/// `closure` is called upon completion, if not cancelled.
init(closure: (R) -> Void) {
self.closure = closure
}
@jonathan-beebe
jonathan-beebe / Example_UITextField+TintClearButton.m
Last active March 14, 2018 17:52
Tinting a UITextField’s clear button
#import "UITextField+TintClearButton.h"
@implementation MyCustomTextFieldClass
- (void)layoutSubviews
{
[super layoutSubviews];
[self tintClearImage];
}
@mackuba
mackuba / wwdc15.md
Last active August 6, 2022 17:28
New stuff from WWDC 2015

Here's my own list of the interesting stuff announced during this year's WWDC, collected from the keynotes, various Apple docs, blog posts and tweets.

If you're planning to watch the videos, I really recommend this Mac app that helps you download and watch them: https://github.com/insidegui/WWDC.

OS X El Capitan

http://www.apple.com/osx/elcapitan-preview/

  • split view - two apps side by side on full screen
@hansott
hansott / TimeAgo.swift
Created December 26, 2014 09:02
Time ago function swift ios cocoa
func timeAgoSinceDate(date:NSDate, numericDates:Bool) -> String {
let calendar = NSCalendar.currentCalendar()
let unitFlags = NSCalendarUnit.CalendarUnitMinute | NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitWeekOfYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitSecond
let now = NSDate()
let earliest = now.earlierDate(date)
let latest = (earliest == now) ? date : now
let components:NSDateComponents = calendar.components(unitFlags, fromDate: earliest, toDate: latest, options: nil)
if (components.year >= 2) {
return "\(components.year) years ago"
} else if (components.year >= 1){