Skip to content

Instantly share code, notes, and snippets.

struct UsernameInputView: View {
@State var username = ""
var body: some View {
TextField(username, text: $username)
}
}
struct UsernameView: View {
let username: String
var body: some View {
Text(username)
}
}
@d-srd
d-srd / FunctionBuilderTesting.swift
Created July 10, 2019 00:16
testing the new Swift function builders for creating a simple routing system
//
// RouteBuilder.swift
// RouteBuilder
//
// Created by Dino on 09/07/2019.
// Copyright © 2019 Dino Srdoc. All rights reserved.
//
import Foundation
//
// ContentView.swift
// SampleUI
//
// Created by Dino on 26/06/2019.
//
// Source for the Observer class: https://www.reddit.com/r/swift/comments/c5pr0z/i_created_a_cool_utility_for_when_youre_working/
// Running this with the Xcode 11 Beta 2 causes the compiler to hang infinitely for me.
// Try it out!
import Foundation
func measure(label: String, printResults output: Bool = true, _ block: @escaping () -> Void) -> Double {
let numberOfTests = 5
var avgExecutionTime : CFAbsoluteTime = 0
for _ in 1...numberOfTests {
let start = CFAbsoluteTimeGetCurrent()
block()
enum Validators {
static let email: (UITextField) -> Bool = { textField in
return textField.text?.trimmed.isValidEmailFormat() ?? false
}
static let phone: (UITextField) -> Bool = { textField in
return textField.text?.trimmed.isValidPhoneNumber() ?? false
}
static let nonEmpty: (UITextField) -> Bool = { textField in
statusItemController.onClick = { statusItem in
DarkMode.toggle()
if DarkMode.isEnabled {
statusItem.button?.title = "on"
} else {
statusItem.button?.title = "off"
}
}
@objc private func performClick(_ sender: Any) {
onClick?(statusItem)
}
init(statusItem: NSStatusItem) {
self.statusItem = statusItem
self.statusItem.button?.target = self
self.statusItem.button?.action = #selector(performClick(_:))
}