Skip to content

Instantly share code, notes, and snippets.

View NikolajMosbaek's full-sized avatar

Nikolaj Simonsen NikolajMosbaek

View GitHub Profile
@NikolajMosbaek
NikolajMosbaek / String+localization.swift
Last active November 23, 2022 08:23
An extension which adds syntactic sugar when using localized strings. You can write: 'let loginTitle = "login_title_key"'
extension String {
var localized: String {
let local = NSLocalizedString(self, comment: "")
guard !local.isEmpty else {
return self
}
return local
}
}
@NikolajMosbaek
NikolajMosbaek / SpaceOperator.swift
Created June 7, 2022 12:05
This is how you can create the spaceship operator found in many other programming languages
infix operator <=>
func <=><T: Comparable>(lhs: T, rhs: T) -> ComparisonResult {
if lhs < rhs { return .orderedAscending}
if lhs > rhs { return .orderedDescending }
return .orderedSame
}
@NikolajMosbaek
NikolajMosbaek / fuzzyFilter.swift
Created June 1, 2022 15:13
With this code you can make an object Searchable and define which parameters that should be included in a search. Then you can easily call .fullyFilter(for: someString) on the object
protocol Searchable {
var keyPaths: [KeyPath<Self, String>] { get }
}
extension Searchable {
func matches(_ searchString: String) -> Bool {
for path in keyPaths {
let value = self[keyPath: path]
if value.localizedCaseInsensitiveContains(searchString) {
@NikolajMosbaek
NikolajMosbaek / fuzzyFilter.swift
Created June 1, 2022 15:13
With this code you can make an object Searchable and define which parameters that should be included in a search. Then you can easily call .fullyFilter(for: someString) on the object
protocol Searchable {
var keyPaths: [KeyPath<Self, String>] { get }
}
extension Searchable {
func matches(_ searchString: String) -> Bool {
for path in keyPaths {
let value = self[keyPath: path]
if value.localizedCaseInsensitiveContains(searchString) {
@NikolajMosbaek
NikolajMosbaek / fuzzyFilter.swift
Created June 1, 2022 15:13
With this code you can make an object Searchable and define which parameters that should be included in a search. Then you can easily call .fullyFilter(for: someString) on the object:
protocol Searchable {
var keyPaths: [KeyPath<Self, String>] { get }
}
extension Searchable {
func matches(_ searchString: String) -> Bool {
for path in keyPaths {
let value = self[keyPath: path]
if value.localizedCaseInsensitiveContains(searchString) {
@NikolajMosbaek
NikolajMosbaek / runFunctionOnValues.swift
Created June 1, 2022 15:03
This code will func a function on every value on any object
func mirror(_ value: Any, using function: (Any) -> Void) {
let mirror = Mirror(reflecting: value)
for child in mirror.children {
function(child.value)
}
}
@NikolajMosbaek
NikolajMosbaek / fuzzyMatch.swift
Created June 1, 2022 14:59
This code will try to match a (search) string to any of an object's string attributes
func ~=(lhs: Any, rhs: String) -> Bool {
let mirror = Mirror(reflecting: lhs)
for child in mirror.children {
guard let value = child.value as? String else { continue }
if value.localizedCaseInsensitiveContains(rhs) {
return true
}
}
@NikolajMosbaek
NikolajMosbaek / PhoneOnlyNavigationView.swift
Created April 27, 2022 12:50
An extension to force stacked navigation style on phones
extension View {
@ViewBuilder func phoneOnlyNavigationView() -> some View {
if UIDevice.current.userInterfaceIdiom == .phone {
self.navigationViewStyle(.stack)
} else {
self
}
}
}
@NikolajMosbaek
NikolajMosbaek / Bundle-Decodable.swift
Created April 27, 2022 11:44
A universal way to decode a bundle
extension Bundle {
func decode<T: Decodable>(_ file: String) -> T {
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Failed to locate \(file) in bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(file) from bundle.")
}
@NikolajMosbaek
NikolajMosbaek / withOptionalAnimation.swift
Last active April 27, 2022 11:46
Use this method to support reduced motion from accessibility
@Environment(\.accessibilityReduceMotion) var reduceMotion
func withOptionalAnimation<Result>(_ animation: Animation? = .default, _ body: () throws -> Result) rethrows -> Result {
if UIAccessibility.isReduceMotionEnabled {
return try body()
} else {
return try withAnimation(animation, body)
}
}