Skip to content

Instantly share code, notes, and snippets.

View KaneCheshire's full-sized avatar

Kane Cheshire KaneCheshire

View GitHub Profile
func logIn() {
// Your code to log in
}
enum Tab {
case profile
case settings
}
func selectTabBar(tab: Tab) {
step("I log in") {
// Your code to log in
}
step("I select the (profile|settings) tab") { (tabType: String) in
switch tabType {
case "profile": // Your code to select the profile tab
case "settings": // Your code to select the settings tab
default: XCTFail()
}
func test_navigatingToSettings() {
Given("I log in")
When("I select the settings tab")
Then("I see the settings screen")
}
Given(I: logIn)
When(I: {
selectTabBar(tab: .settings)
})
Then(I: seeSettingsScreen)
struct Step {
@discardableResult
init(I handler: () -> Void) {
handler()
}
@discardableResult
init(I handler: @autoclosure () -> Void) {
handler()
func test_navigatingToSettings() {
Given(I: logIn)
When(I: selectTabBar(tab: .settings))
Then(I: seeSettingsScreen)
}
class Cell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
let settings = Settings() // This is me being sneaky, you really need to get this from a Realm
accessoryView = SettingsSwitch(object: settings, keyPath: \Settings.prefersFahrenheit)
}
}
let settingsSwitch = SettingsSwitch(object: settings, keyPath: \Settings.prefersFahrenheit)
class SettingsSwitch<T>: UISwitch {
typealias BoolWritableKeyPath = WritableKeyPath<T, Bool>
private var object: T // This is the object we'll update when the value changes
private let keyPath: BoolWritableKeyPath // This is the key path we'll use to update the object
init(object: T, keyPath: BoolWritableKeyPath) {
self.object = object
self.keyPath = keyPath
import RealmSwift
class Settings: Object {
@objc private dynamic var _prefersFahrenheit: Bool = false // Realm requires all properties have a default value
var prefersFahrenheit: Bool { // I find it's best to define explicit getters and setters since you can transform primitives into enums etc.
get {
return _prefersFahrenheit
}