Skip to content

Instantly share code, notes, and snippets.

let regex = try NSRegularExpression(pattern: mypattern, options: [])
let str = "some string to match"
if let result = regex.firstMatchInString(str, options: [], range: NSMakeRange(0, str.characters.count)) {
print((str as NSString).substringWithRange((result?.rangeAtIndex(1))!))
}
protocol BaseSection {
var cellIdentifier: String { get }
func numberOfRows() -> Int
func registerIdentifier(tableView: UITableView)
func configureCell(cell: UITableViewCell, indexPath: NSIndexPath)
}
class Section<T, Cell: UITableViewCell>: BaseSection {
var rows: [T] = []
var cellIdentifier = ""
struct TableViewSection<T, Cell: UITableViewCell> {
var cellIdentifier: String = ""
var items: [T] = []
var configureCell: (cell: Cell, item: T, indexPath: NSIndexPath) -> Void
}
class TableViewDataSource: NSObject, UITableViewDataSource {
var sections: [TableViewSection<?, ?>] = [] // I want arbitrary T/Cell types here, what do?
// ...
import UIKit
class ViewController: UIViewController {
// error: 'UISearchController' is only available on iOS 8.0 or newer
var searchController: UISearchController?
// error: Stored properties cannot be marked potentially unavailable with 'introduced='
@available(iOS 8.0, *)
var conditionallyAvailableSearchController: UISearchController?
@klundberg
klundberg / weakify.swift
Last active May 13, 2020 08:22
Weakify functions to help you weakly bind instances to static method references
// these functions take a swift class's statically referenced method and the instance those methods
// should apply to, and returns a function that weakly captures the instance so that you don't have
// to worry about memory retain cycles if you want to directly use an instance method as a handler
// for some object, like NSNotificationCenter.
//
// For more information, see this post:
// http://www.klundberg.com/blog/capturing-objects-weakly-in-instance-method-references-in-swift/
func weakify <T: AnyObject, U>(owner: T, f: T->U->()) -> U -> () {
return { [weak owner] obj in