Skip to content

Instantly share code, notes, and snippets.

@wokalski
wokalski / gist:3104114
Created July 13, 2012 10:27
Divide long string into pieces to fit a width . -(void) divideLongString:(NSString *) string toFitWidth:(CGFloat) width withFont: (UIFont *) font
-(NSMutableArray*) divideLongString:(NSString *) string toFitWidth:(CGFloat) width withFont: (UIFont *) font {
NSMutableArray* retArray = [NSMutableArray array];
CGSize stringSize = [string sizeWithFont:font];
float ratio = width/stringSize.width;
int difference = (int) stringSize.width % (int) width;
int numberOfLines = (stringSize.width - difference) /width;
@wokalski
wokalski / gist:3130397
Created July 17, 2012 16:21
NSRangeContainsRange - check if NSRange contains the other NSRange :)
bool NSRangeContainsRange (NSRange range1, NSRange range2) {
BOOL retval = NO;
if (range1.location <= range2.location && range1.location+range1.length >= range2.length+range2.location) {
retval = YES;;
}
return retval;
}
@wokalski
wokalski / gist:3130403
Created July 17, 2012 16:21
NSRangeContainsRange - check if NSRange contains the other NSRange :)
bool NSRangeContainsRange (NSRange range1, NSRange range2) {
BOOL retval = NO;
if (range1.location <= range2.location && range1.location+range1.length >= range2.length+range2.location) {
retval = YES;;
}
return retval;
}
(lldb) po aView
<NSTextField: 0x600000199710>
(lldb) po aView
<NSImageView: 0x600000179380>
(lldb) po aView
<NSView: 0x60800013b440>
(lldb) bt all
@wokalski
wokalski / typealias.md
Last active March 12, 2016 19:16
Patch proposal (idea validation)

I suggest the following (minor(?)) extension of typealias capabilities:

typealias ExampleAlias = CollectionType where Generator.Element : Equatable, Index : SignedIntegerType

Rationale:

  1. Lengthy constraints in generic functions:
func foo<T: CollectionType where T.Generator.Element : Equatable, T.Index : SignedIntegerType>(a a: T, b: T)
@wokalski
wokalski / UIBezierPath+shapeLayer.swift
Created March 25, 2016 20:46
Create a CAShapeLayer with a UIBezierPath
extension UIBezierPath {
func shapeLayer() -> CAShapeLayer {
let l = CAShapeLayer()
let path = self.copy()
let bounds = path.bounds
let origin = bounds.origin
path.applyTransform(CGAffineTransformMakeTranslation(-origin.x, -origin.y))
l.path = path.CGPath
struct Queue<T> {
private var elements: [T] = []
mutating func enqueue(element: T) {
elements.append(element)
}
mutating func enqueue(elements: [T]) {
elements.forEach {self.elements.append($0)}
}
@wokalski
wokalski / Result.js
Last active May 2, 2017 23:26
Result type in javascript with flow
/**
* @flow
* @providesModule Result
*/
'use strict'
export const SuccessType = 'SuccessType'
export const ErrorType = 'ErrorType'
type SuccessT<T> = T & {
@wokalski
wokalski / NestedDiff.swift
Created December 22, 2016 22:08
A too complicated implementation of NestedDiff (diffing collections containing collections (i.e. arrays containing arrays).
public struct NestedDiff: DiffProtocol {
public enum Element {
case deleteSection(Int)
case insertSection(Int)
case deleteRow(Int, section: Int)
case insert(row: Int, section: Int)
}
@import Foundation;
NS_ASSUME_NONNULL_BEGIN
@interface Node <T>: NSObject
@property (nonatomic, strong, readonly) T value;
@property(nonatomic, strong, readonly) Node<T> *next;
- (instancetype)initWithValue:(T)value next:(nullable Node<T> *)next;