This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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> | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let navigationBarAppearance = UINavigationBar.appearance() | |
let previousColor = navigationBarAppearance.barTintColor | |
navigationBarAppearance.barTintColor = .whiteColor() | |
let mailCompose = MFMailComposeViewController() | |
navigationBarAppearance.barTintColor = previousColor |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")!] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func beginsWith(prefix: String) -> Bool { | |
return count(self) >= count(prefix) | |
&& substringWithRange( Range<String.Index>(start: startIndex, end: advance(startIndex, count(prefix))) ) == prefix | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var qp = 0.0 | |
for var n=0.0; ;++n { | |
qp += (1-(n%2)*2)/(2*n+1) | |
println("\(qp*4)") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |