Skip to content

Instantly share code, notes, and snippets.

View klein-artur's full-sized avatar

Artur Hellmann klein-artur

View GitHub Profile
@klein-artur
klein-artur / Selectables.swift
Created July 8, 2023 00:13
SwiftUI Selectable List
struct Selectables<Data, ID: Hashable, Content>: View where Content: View {
let data: [Data]
@Binding var selectedIds: [ID]
let id: KeyPath<Data, ID>
let content: (Data, Binding<Bool>) -> Content
init(_ data: [Data], selectedIds: Binding<[ID]>, id: KeyPath<Data, ID>, @ViewBuilder content: @escaping (Data, Binding<Bool>) -> Content) {
self.data = data
self._selectedIds = selectedIds
self.id = id
@klein-artur
klein-artur / testableSwiftResult.swift
Created January 19, 2023 00:42
Checking Result for success or error in Tests
extension Result where Failure == ParseError {
func checkError(messageIfNotError: String, type: ParseErrorType) {
switch self {
case .success(_):
XCTFail(messageIfNotError)
case let .failure(error):
if error.type != type {
XCTFail("Should have thrown \(type.rawValue) but did \(error.type)")
}
}
@klein-artur
klein-artur / GeneralAlert.swift
Created January 10, 2023 23:00
An alert modifier for better alert handling in SwiftUI
struct AlertAction {
let title: String
var role: ButtonRole? = .none
let action: (() -> Void)
}
struct AlertItem {
let title: String
let message: String
let actions: [AlertAction]
@klein-artur
klein-artur / CustomAlert.swift
Created July 9, 2022 12:40
A viewmodifier for SwiftUI to make handling multiple alerts in one view file much easier.
import Foundation
import SwiftUI
struct AlertButton: Identifiable {
var id: String {
title
}
let title: String
let style: ButtonRole?
@klein-artur
klein-artur / String+Regex.swift
Last active June 15, 2021 06:25
Some String extensions for regex
struct RgxResult {
private let textCheckingResult: NSTextCheckingResult
private let baseString: String
init(_ result: NSTextCheckingResult, _ baseString: String) {
self.textCheckingResult = result
self.baseString = baseString
}
}
@klein-artur
klein-artur / Rx+TwoWayBinding.swift
Created February 17, 2020 17:58
Two Way Rx Binding with an infix operator <->
import Cocoa
import RxSwift
import RxCocoa
infix operator <->
func <-> <T>(property: ControlProperty<T>, variable: BehaviorRelay<T>) -> Disposable {
let bindToUIDisposable = variable.asObservable()
.bind(to: property)
@klein-artur
klein-artur / stringfinder.md
Last active August 21, 2019 12:07
Regex to find strings

Find Strings between quotes

(?:")((?:.|\n)*?)(?<!\\)(?:")

The first group will be the string itself. Escaped quotes are ignored.

@klein-artur
klein-artur / String+Localization.swift
Last active March 10, 2019 21:15
Simple way to localize your String. I don't know why, but somehow I hate alway entering this NSLocalizedString methods. Do your stuff easy with this extension by just "localize your string".localize()
extension String {
func localize() -> String {
return NSLocalizedString(self, comment: "")
}
func localize(_ args: CVarArg...) -> String {
return String(format: NSLocalizedString(self, comment: ""), args)
}
}
@klein-artur
klein-artur / urlSplitterRegex.md
Last active January 10, 2022 18:56
Regex to split an url into its parts:

Regex to split an url into its parts:

This regex splits an http or https url into its parts.

^(?:(?:(?:(http[s]?):\/\/)(?:([a-zA-Z0-9]+)?\.)?([^:\/\s\?]+\.+[^:\/\s\?]+))?(?:((?:\/[^\/\s]+)*\/)?(?:([\w\-\.]+[^#?\s]+))?)?(#[^#?\s]+)?(?:\?+([^\.\s$]+)?)?)$

Groups

  1. Whole match
@klein-artur
klein-artur / StringExtensionSubstring.swift
Created October 24, 2016 11:32
String extension to get a substring by UInt indexes.
extension String {
func substring(from: UInt, length: UInt) -> String {
if Int(from) >= self.characters.count {
preconditionFailure("from is out of bounds.")
}
if Int(from+length) > self.characters.count {
preconditionFailure("from and lenght are out of bounds")
}
let startIndex = self.index(self.startIndex, offsetBy: String.IndexDistance(from))