This file contains 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 validate<T: Codable>(_ validator: @escaping (T) -> Bool) -> (T) -> Bool { | |
return { input in | |
return validator(input) | |
} | |
} | |
enum StringValidator: CustomStringConvertible { | |
case isNotEmpty | |
case lengthIn(ClosedRange<Int>) | |
This file contains 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
function prompt_char { | |
if [ $UID -eq 0 ]; then echo "#"; else echo $; fi | |
} | |
PROMPT='%(!.%{$fg_bold[red]%}.%{$fg_bold[cyan]%}%n@)%m:%{$fg_bold[green]%}%(!.%~.%1~)%{$fg_bold[yellow]%} $(git_prompt_info)%{$fg_bold[cyan]%}$(prompt_char)%{$reset_color%} ' | |
ZSH_THEME_GIT_PROMPT_PREFIX="(" | |
ZSH_THEME_GIT_PROMPT_SUFFIX=") " |
This file contains 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 | |
struct LocationData: Codable { | |
var city: String | |
var country: String | |
var address: String? | |
} | |
struct Item: Codable { | |
var name: String |
This file contains 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 UIKit | |
final class SDPresentationManager: NSObject, UIViewControllerTransitioningDelegate { | |
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? { | |
return SDModalPresentationController(presentedViewController: presented, presenting: source) | |
} | |
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { | |
return SDModalTransitionPresentationAnimator() |
This file contains 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 showPushNotification(title: String, details: String) { | |
if #available(iOS 10.0, *) { | |
let interval = TimeInterval(1) | |
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false) | |
let content = UNMutableNotificationContent() | |
content.title = title | |
content.body = details | |
let req = UNNotificationRequest(identifier: "localPushNotification", content: content, trigger: trigger) | |
let center = UNUserNotificationCenter.current() | |
center.getNotificationSettings(completionHandler: { settings in |
This file contains 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 View { | |
func iff(_ condition: Bool, _ modifier: (Self) -> AnyView) -> AnyView { | |
if condition { | |
return modifier(self).eraseToAnyView() | |
} | |
return eraseToAnyView() | |
} | |
func some<Value>(_ optional: Value?, modifier: (Value, Self) -> AnyView) -> some View { | |
guard let value = optional else { |
This file contains 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 UIKit | |
extension UIImage { | |
/** | |
Resizes the image to width x height and converts it to an RGB CVPixelBuffer. | |
*/ | |
public func pixelBuffer(width: Int, height: Int) -> CVPixelBuffer? { | |
var maybePixelBuffer: CVPixelBuffer? | |
let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue, | |
kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue] |
This file contains 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 NSObjectProtocol where Self: NSObject { | |
func observe<Value>(_ keyPath: KeyPath<Self, Value>, onChange: @escaping (Value) -> ()) -> NSKeyValueObservation { | |
return observe(keyPath, options: [.initial, .new]) { _, change in | |
// TODO: change.newValue should never be `nil`, but when observing an optional property that's set to `nil`, then change.newValue is `nil` instead of `Optional(nil)`. This is the bug report for this: https://bugs.swift.org/browse/SR-6066 | |
guard let newValue = change.newValue else { return } | |
onChange(newValue) | |
} | |
} | |
func bind<Value, Target>(_ sourceKeyPath: KeyPath<Self, Value>, to target: Target, at targetKeyPath: ReferenceWritableKeyPath<Target, Value>) -> NSKeyValueObservation { |
This file contains 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
@dynamicMemberLookup | |
class Person { | |
let name: String | |
let age: Int | |
private let details: [String: String] | |
init(name: String, age: Int, details: [String: String]) { | |
self.name = name | |
self.age = age | |
self.details = details |
This file contains 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
//: # Swift 3: Working with dates | |
import Foundation | |
let date = Date() | |
let myLocale = Locale(identifier: "bg_BG") | |
//: ### Setting an application-wide `TimeZone` | |
//: Notice how we use if-let in case the abbreviation is wrong. It will fallback to the default timezone in that case. | |
if let myTimezone = TimeZone(abbreviation: "EEST") { | |
print("\(myTimezone.identifier)") |