Skip to content

Instantly share code, notes, and snippets.

View indragiek's full-sized avatar

Indragie Karunaratne indragiek

View GitHub Profile
//
// JumpFixTableViewController.swift
//
// Created by Indragie on 1/3/15.
// Copyright (c) 2015 Indragie Karunaratne. All rights reserved.
//
import UIKit
/// Table view controller that implements a workaround for a bug where
@indragiek
indragiek / CGFloatFormattingBug.swift
Created January 29, 2015 02:48
Swift bug when formatting CGFloats
import Cocoa
// Doesn't work for CGFloats
let a: CGFloat = 1.0
let b: CGFloat = 2.0
String(format: "%f %f", a, b) // "0.000000 0.000000"
// Works for Floats
let c: Float = 1.0
let d: Float = 2.0
@indragiek
indragiek / generics.swift
Last active August 29, 2015 14:13
This code doesn't compile (Xcode 6.1.1, Swift 1.1)
public protocol TableViewSectionType {
typealias ItemCollection: CollectionType
}
public protocol TableViewCellFactoryType {
typealias Item
}
public final class TableViewDataSource<
SectionCollection: CollectionType,
@indragiek
indragiek / gist:88a148c7006aca6c3dbf
Created January 3, 2015 04:14
UITextView crash
* thread #1: tid = 0x4ec0b, 0x0000000102444b8a libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x0000000102444b8a libobjc.A.dylib`objc_exception_throw
frame #1: 0x00000001027abe6d CoreFoundation`+[NSException raise:format:] + 205
frame #2: 0x00000001066f79f6 UIFoundation`-[NSConcreteTextStorage attribute:atIndex:effectiveRange:] + 131
frame #3: 0x00000001066c351b UIFoundation`-[NSAttributedString(NSAttributedStringUIFoundationAdditions) rangeOfTextTable:atIndex:] + 65
frame #4: 0x00000001066f9e1b UIFoundation`-[NSTextBlockLayoutHelper initWithTextTable:charIndex:text:layoutManager:containerWidth:collapseBorders:] + 376
frame #5: 0x00000001066fe85f UIFoundation`-[NSTextTable rectForBlock:layoutAtPoint:inRect:textContainer:characterRange:] + 355
frame #6: 0x00000001066fcc3b UIFoundation`-[NSTextTableBlock rectForLayoutAtPoint:inRect:textContainer:characterRange:] + 116
frame #7: 0x0000000106706b3b UIFoundation`-[NSType

Keybase proof

I hereby claim:

  • I am indragiek on github.
  • I am indragie (https://keybase.io/indragie) on keybase.
  • I have a public key whose fingerprint is 9B62 C6E3 9E0D C285 1423 1EB3 3FA7 3F1A E47E 67A6

To claim this, I am signing this object:

@indragiek
indragiek / DynamicTypeLabel.swift
Last active August 25, 2017 15:08
UILabel subclass that automatically adjusts the font when the global dynamic type setting changes.
//
// DynamicTypeLabel.swift
//
// Created by Indragie on 10/16/14.
// Copyright (c) 2014 Indragie Karunaratne. All rights reserved.
//
import UIKit
class DynamicTypeLabel : UILabel {
@indragiek
indragiek / RandomNumbers.swift
Last active November 26, 2015 16:26
Random number generator in Swift
public struct RandomNumberGenerator: SequenceType {
let range: Range<Int>
let count: Int
public init(range: Range<Int>, count: Int) {
self.range = range
self.count = count
}
public func generate() -> GeneratorOf<Int> {
@indragiek
indragiek / KeypathExtensions.swift
Last active August 29, 2015 14:07
Implementation of -valueForKeyPath: that supports indexed subscripts, implemented in Swift.
//
// KeypathExtensions.swift
//
// Created by Indragie on 10/3/14.
// Copyright (c) 2014 Indragie Karunaratne. All rights reserved.
//
import Foundation
let KeyRegex = NSRegularExpression(pattern: "^(\\w[\\w\\d]*)?(\\[\\d+\\])*$", options: NSRegularExpressionOptions(0), error: nil)
@indragiek
indragiek / gist:0b163d8a1d998aa44ff6
Last active August 29, 2015 14:02
Swift compiler segfault when accessing computed property on class extension
struct SomeStruct {
let value: String
}
extension UIView {
var ind_someProperty: SomeStruct {
return SomeStruct(value: "Broken compiler")
}
}
@indragiek
indragiek / LTClientBrowser.h
Last active August 29, 2015 14:01
ReactiveCocoa-based wrapper around NSNetServiceBrowser
//
// LTClientBrowser.h
// LayerTreeServer
//
// Created by Indragie Karunaratne on 2014-05-21.
// Copyright (c) 2014 Indragie Karunaratne. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <LayerTreeKit/LayerTreeKit.h>