Skip to content

Instantly share code, notes, and snippets.

View isoiphone's full-sized avatar

Jacob Schwartz isoiphone

View GitHub Profile
import Foundation
// based on: http://stackoverflow.com/questions/18118021/how-to-resize-superview-to-fit-all-subviews-with-autolayout
// required so we can have multiline labels properly resize their height based on content
class MultilineLabel: UILabel {
override func layoutSubviews() {
super.layoutSubviews()
if numberOfLines == 0 && preferredMaxLayoutWidth != CGRectGetWidth(frame) {
@isoiphone
isoiphone / gist:4467bceb82dcd1d08af2
Created May 22, 2015 04:59
Trying and failing at generics
// trying to add objectsAtIndexes() to compatible swift arrays, such as [Int]
// compiler error: 'Array<T>' is not convertible to 'NSArray'
extension Array {
func objectsAtIndexes<T: AnyObject>(indexes: NSIndexSet) -> Array<T> {
return (self as NSArray).objectsAtIndexes(indexes) as! Array<T>
}
}
@isoiphone
isoiphone / gist:0a1e20d0ffa7bd28073e
Created May 13, 2015 06:55
This seems to be the ONLY way to change a bar tint color of a MFMailComposeViewController (on iOS 8)
let navigationBarAppearance = UINavigationBar.appearance()
let previousColor = navigationBarAppearance.barTintColor
navigationBarAppearance.barTintColor = .whiteColor()
let mailCompose = MFMailComposeViewController()
navigationBarAppearance.barTintColor = previousColor
dispatch_once(&_migrationToken) {
// scorched-earth data migration policy
RLMRealm.setSchemaVersion(1, forRealmAtPath: path) { (migration, oldSchemaVersion) in
for schema in migration.oldSchema.objectSchema {
migration.enumerateObjects(schema.className) { (oldObject, newObject) in
migration.deleteObject(newObject)
}
}
}
}
func configureRealm() {
let basePath = RLMRealm.defaultRealmPath()
println("realm path: \(basePath)")
let allPaths = [basePath,
basePath.stringByAppendingPathExtension("lock")!,
basePath.stringByAppendingPathExtension("log")!,
basePath.stringByAppendingPathExtension("log_a")!,
basePath.stringByAppendingPathExtension("log_b")!]
@isoiphone
isoiphone / gist:f779092822df6cc0bb8d
Created March 25, 2015 00:54
ARrrgg compile speed
// takes ~50 seconds to compile.
// tested on XCode 6.3 Beta 4, late 2013 macbook pro retina
import Foundation
func CompileSmash() {
var arr: [AnyObject] = [1,2,3]
arr = arr + arr + arr + arr + arr
}
@isoiphone
isoiphone / gist:2d79b2fdfc6e9008d40c
Created March 19, 2015 00:44
string beginsWith
extension String {
func beginsWith(prefix: String) -> Bool {
return count(self) >= count(prefix)
&& substringWithRange( Range<String.Index>(start: startIndex, end: advance(startIndex, count(prefix))) ) == prefix
}
}
var p = 0.0
for var k=0.0; k<100.0;++k {
let rest: Double = 4.0/(8*k+1) - 2.0/(8*k+4) - 1.0/(8*k+5) - 1.0/(8*k+6)
p += pow(16.0, -k) * rest
println("\(p)")
}
var qp = 0.0
for var n=0.0; ;++n {
qp += (1-(n%2)*2)/(2*n+1)
println("\(qp*4)")
}
@isoiphone
isoiphone / gist:fd907d9c2e02838200f1
Created March 12, 2015 00:30
Manual thread storage, may stick with this pattern.
extension RLMRealm {
static var threadInstance: RLMRealm! {
let threadDictionary = NSThread.currentThread().threadDictionary
if let inst = threadDictionary["SharedRealmInst"] as! RLMRealm? {
return inst
} else {
let inst = defaultRealmWithErrorHandling()
threadDictionary["SharedRealmInst"] = inst
return inst
}