Skip to content

Instantly share code, notes, and snippets.

View hartbit's full-sized avatar
👨‍💻
Swifting, one commit at a time

David Hart hartbit

👨‍💻
Swifting, one commit at a time
  • Atipik Sàrl & Witty Wings SA
  • Geneva, Switzerland
View GitHub Profile
@hartbit
hartbit / gist:81ff82025482f3b240d0
Last active August 29, 2015 14:23
This is so weird
extension UIView {
var firstResponder: UIView? {
if self.isFirstResponder() {
return self
}
for subview in self.subviews {
if let fr = subview.firstResponder /*as UIView? -- FIX */ {
assert(fr != nil, "THIS SHOULD NOT HAPPEN BUT DOES")
return fr
//: Playground - noun: a place where people can play
import UIKit
protocol PlaceholderText : class, UITextInputTraits {
var placeholder: String? { get set }
}
extension UITextField: PlaceholderText { }
extension UITextView: PlaceholderText {
enum Enum : Equatable {
case Case1
case Case2(variable: AnyObject)
}
let a = Enum.Case1
let b = a == Enum.Case1
func == (e1: Enum, e2: Enum) -> Bool {
switch e1 {
extension Nat: IntegerLiteralConvertible {
init(integerLiteral value: IntegerLiteralType) {
switch (value) {
case 0:
self = .Zero
default:
self = .Succ(Nat(integerLiteral: value - 1))
}
}
}
// without double escapes: \A([^{}]*)(?:\{([^{}/]*)(?:\/([^{}/]*))+\})?([^{}]*)\z
let formatRegex = NSRegularExpression(pattern: "\\A([^{}]*)(?:\\{([^{}/]*)(?:/([^{}/]*))+\\})?([^{}]*)\\z", options: .allZeros, error: nil)
let test = "In this line {this should be captured/and this but it isn’t/and finally this}. I don’t understand."
// Trying to write a function that does some select/map magic with easy error handling
let cities = [[
"name": "Geneva",
"population": 184538
],[
"name": "Bern",
"population": 123154
], [
"name": "Zurich",
@interface MyClass : NSObject
@property (nonatomic) BOOL boolProperty;
@end
@implementation MyClass
@end
// ...
MyClass* obj = [MyClass new];
protocol Delegate<T> {
func generate() -> T
}
class Controller<T> {
var delegate: Delegate<T>
}
protocol TableControllerDelegate {
typealias ItemType
func tableViewCellForItem(item: ItemType, atIndexPath indexPath: NSIndexPath) -> UITableViewCell
}
class TableController<T> : NSObject, UITableViewDataSource, UITableViewDelegate {
var items: [T]?
var delegate: TableControllerDelegate?
//MARK: UITableViewDataSource & UITableViewDelegate Methods
@hartbit
hartbit / update_storyboard_strings.rb
Created July 24, 2014 12:37
Script to update Storyboard localisation strings in Base Internalization for Xcode
#!/usr/bin/ruby
#
# update_storyboard_strings.rb - automatically extract translatable strings from storyboards and update strings files
# Based on:
# - http://forums.macrumors.com/showpost.php?p=16060008&postcount=4 by mikezang
# - http://oleb.net/blog/2013/02/automating-strings-extraction-from-storyboards-for-localization/ by Ole Begemann
require 'Tempfile'
Dir.glob('**/Base.lproj/*.storyboard') do |storyboard_path|