Skip to content

Instantly share code, notes, and snippets.

@harlanhaskins
harlanhaskins / StringScanner.swift
Last active August 4, 2020 23:45
Swift String Scanner
import Foundation
/// `StringScanner` is a fast scanner for Strings and String-like objects.
/// It's used to extract structured bits from unstructured strings, while
/// avoiding making extra copies of string bits until absolutely necessary.
/// You can build Scanners over Substrings, allowing you to scan
/// parts of strings and use smaller, more specialized scanners to extract bits
/// of that String without needing to reuse another scanner.
public struct StringScanner<Input: StringProtocol> {
let input: Input
@vinhnx
vinhnx / uicollectionview+centerPaging.md
Created June 9, 2017 09:24
Centered Paging with Preview Cells on UICollectionView

Centered Paging with Preview Cells on UICollectionView

The proposed offset is where the collection view would stop without our intervention. We peek into this area by finding its centre as proposedContentOffsetCenterX and examine our currently visible cells to see which one’s centre is closer to the centre of that area.

import UIKit
 
class CenterCellCollectionViewFlowLayout: UICollectionViewFlowLayout {
    
    var mostRecentOffset : CGPoint = CGPoint()
@maxchuquimia
maxchuquimia / Example.swift
Created June 8, 2017 00:10
Paging a UICollectionView when each cell is smaller than the width of the collection
// Set the inset so that the first and last cells can appear in the middle of the UICollectionView
override func layoutSubviews() {
super.layoutSubviews()
contentInset = UIEdgeInsets(top: 0, left: frame.width / 2.0 - layout.itemSize.width / 2.0, bottom: 0, right: 0)
}
private func currentCentredCell() -> UICollectionViewCell? {
return visibleCells.reduce(visibleCells.first) { (nearestCell, nextCell) -> UICollectionViewCell? in
import Foundation
extension Dictionary {
mutating public func setValue(val: AnyObject, forKeyPath keyPath: String) {
var keys = keyPath.componentsSeparatedByString(".")
guard let first = keys.first as? Key else { print("Unable to use string as key on type: \(Key.self)"); return }
keys.removeAtIndex(0)
if keys.isEmpty, let settable = val as? Value {
self[first] = settable
} else {
@chriseidhof
chriseidhof / Main.swift
Last active March 11, 2016 06:59
Protocol Extensions
protocol OptionalType {
typealias T
var optional: T? { get }
}
extension Optional : OptionalType {
var optional: T? { return self }
}
extension SequenceType where Generator.Element: OptionalType {
@phynet
phynet / Generate and Install IPA's file in device through Command Line.md
Last active December 22, 2022 16:15
Generate and Install IPA's file in device through Command Line

##Generate and Install IPA's file in device through Command Line

###Thanks to Mattt and phonegap's scripts

Take a note: all this steps can be automatized in a single script, if you know how to write it. (Bash or Ruby will be fine)

1.- Install your Provisioning Profile and Developer Certificate

2.- Install Shenzhen and ios-deploy: the firstone will generate the IPA file and the secondone will install it onto your device

@andymatuschak
andymatuschak / CollectionViewDataSource.swift
Last active February 12, 2021 09:44
Type-safe value-oriented collection view data source
//
// CollectionViewDataSource.swift
// Khan Academy
//
// Created by Andy Matuschak on 10/14/14.
// Copyright (c) 2014 Khan Academy. All rights reserved.
//
import UIKit
@staltz
staltz / introrx.md
Last active November 19, 2024 18:00
The introduction to Reactive Programming you've been missing
@janodev
janodev / MySingleton.h
Last active July 28, 2016 03:19
Singleton pattern with __atribute__ unavailable to prevent the accidental misuse of a singleton. Note that the user is still able to create more instances using runtime functions.
#import <Foundation/Foundation.h>
@interface MySingleton : NSObject
+(instancetype) sharedInstance;
// clue for improper use (produces compile time error)
+(instancetype) alloc __attribute__((unavailable("alloc not available, call sharedInstance instead")));
-(instancetype) init __attribute__((unavailable("init not available, call sharedInstance instead")));
+(instancetype) new __attribute__((unavailable("new not available, call sharedInstance instead")));
@bjhomer
bjhomer / controller.m
Last active December 14, 2015 07:59
Pattern for -init methods of controllers designed in a storyboard
- (id)init
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainInterface" bundle:nil];
self = [storyboard instantiateViewControllerWithIdentifier:@"MyControllerIdentifier"];
// Set up ivars here. 'self' won't be nil; if it is, you passed an invalid identifier
// and UIKit would have thrown an exception anyway.
return self;
}