Skip to content

Instantly share code, notes, and snippets.

View MojtabaHs's full-sized avatar

Seyed Mojtaba Hosseini Zeidabadi MojtabaHs

View GitHub Profile
@MojtabaHs
MojtabaHs / IranianNationalCodeValidator.swift
Created December 15, 2020 15:14
The logic to validate Iranian National Code
func validateNationalCode(_ code: String) -> Bool {
let stringCode = code.trimmingCharacters(in: CharacterSet.decimalDigits.inverted)
if code.allSatisfy({ $0 == code.first }) { return false }
guard stringCode.count == 10 else { return false }
var convertedCode = code.compactMap { Int(String($0)) }
guard convertedCode.count == 10 else { return false }
@MojtabaHs
MojtabaHs / nextFirstResponder.swift
Created December 4, 2020 22:24
Finds and returns the next responder with the given condition.
extension UIResponder {
func nextFirstResponder(where condition: (UIResponder) -> Bool ) -> UIResponder? {
guard let next = next else { return nil }
if condition(next) { return next }
else { return next.nextFirstResponder(where: condition) }
}
}
@MojtabaHs
MojtabaHs / InlineObjectModifierOperator.swift
Created November 7, 2020 10:17
A simple operator to modify the object right at the initialization part.
infix operator ..
@inline(__always)
func ..<T: EmptyInitializable>(lhs: T, rhs: (T)->()) -> T {
rhs(lhs)
return lhs
}
/* Example:
@MojtabaHs
MojtabaHs / SettingsBundleStorage.swift
Created November 6, 2020 17:39
A property wrapper for easy access to the settings bundle.
@propertyWrapper
public struct SettingsBundleStorage<T> {
private let key: String
private let userDefaults: UserDefaults = .standard
public init(key: String) {
self.key = key
setBundleDefaults(plist: .root)
@MojtabaHs
MojtabaHs / UserDefaultsStorage.swift
Last active March 1, 2024 09:55
A property. wrapper for store/restore variables backed by the given user defaults.
import Foundation
@propertyWrapper
public struct UserDefaultStorage<T: Codable> {
private let key: String
private let defaultValue: T
private let userDefaults: UserDefaults
public init(key: String, default: T, store: UserDefaults = .standard) {
@MojtabaHs
MojtabaHs / ViewRepresentableHelper.swift
Created September 24, 2020 13:20
Representing both AppKit and UIKit Views inside the SwiftUI
#if os(macOS)
public typealias ViewRepresentable = NSViewRepresentable
public typealias NativeView = NSView
#elseif os(iOS)
public typealias ViewRepresentable = UIViewRepresentable
public typealias NativeView = UIView
#endif
public protocol ViewRepresentableHelper: ViewRepresentable {
associatedtype ViewType: NativeView
@MojtabaHs
MojtabaHs / PropertyWrapper-BundleFile.swift
Last active August 5, 2020 08:19
A PropertyWrapper for load and decode any Decodable from the given bundle.
@propertyWrapper struct BundleFile<DataType> {
let name: String
let type: String
let fileManager: FileManager = .default
let bundle: Bundle = .main
let decoder: (Data) -> DataType
var wrappedValue: DataType {
guard let path = bundle.path(forResource: name, ofType: type) else { fatalError("Resource not found") }
guard let data = fileManager.contents(atPath: path) else { fatalError("File not loaded") }
@MojtabaHs
MojtabaHs / MaskableView.swift
Created August 1, 2020 22:14
A class for masking any subclass of UIView with an Image.
@IBDesignable
class MaskableView: <#AnyUIViewSubclass#> {
var maskImageView = UIImageView()
@IBInspectable
var maskImage: UIImage? {
didSet {
maskImageView.image = maskImage
updateView()
}
@MojtabaHs
MojtabaHs / Faillible.swift
Created July 9, 2020 11:19
A property wrapper for arrays with invalid decodable elements.
@propertyWrapper
struct Fallible<Value: Decodable>: Decodable {
var wrappedValue: [Value] = []
private struct _None: Decodable {}
init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
if let decoded = try? container.decode(Value.self) {