Skip to content

Instantly share code, notes, and snippets.

View MMnasrabadi's full-sized avatar
🤠
i am happy

Mohammad Mohammadi Nasrabadi MMnasrabadi

🤠
i am happy
View GitHub Profile
// Xcode 11b1
@propertyDelegate
struct Clamped<Value: Comparable> {
private var storage: Value
private var clamp: (Value) -> Value
init(min: Value, max: Value, initialValue: Value) {
let clampingFunction = { ($0...$0).clamped(to: min...max).lowerBound }
self.storage = clampingFunction(initialValue)
enum Demo {
case simple
case oneValue(Int)
case twoValues(String, Double)
case threeValues(one: Int, two: Float, [Int])
}
//: # Direct exposition in the enum
//: ## Sourcery Template
@MMnasrabadi
MMnasrabadi / Bindings.swift
Created June 20, 2020 11:39 — forked from AliSoftware/Bindings.swift
Re-implementation of @binding and @State (from SwiftUI) myself to better understand it
/*:
This is a concept re-implementation of the @Binding and @State property wrappers from SwiftUI
The only purpose of this code is to implement those wrappers myself
just to understand how they work internally and why they are needed,
⚠️ This is not supposed to be a reference implementation nor cover all
subtleties of the real Binding and State types.
The only purpose of this playground is to show how re-implementing
them myself has helped me understand the whole thing better
@MMnasrabadi
MMnasrabadi / CopyLabel.swift
Created June 20, 2020 11:42 — forked from AliSoftware/CopyLabel.swift
An UILabel subclass which allows you to show the "copy" MenuController item to copy its content to the pasteboard
import UIKit
class CopyLabel : UILabel {
// MARK: Setup
override init(frame: CGRect) {
super.init(frame: frame)
configureMenu()
}
func using<T: AnyObject>(object: T, execute: (T) throws -> Void) rethrows -> T {
try execute(object)
return object
}
import UIKit
// Then in some configureView() function of an UIViewController or whatnot…
let label1 = using(UILabel()) {
# Property Wrappers
https://github.com/apple/swift-evolution/blob/master/proposals/0258-property-wrappers.md
@MMnasrabadi
MMnasrabadi / iranianBankCardCheck.swift
Created June 30, 2020 05:10
Check iranian Bank Card Validation
// Create by love : Mohammad Nasrabadi [github](https://github.com/mmnasrabadi)
iranianBankCardCheck(5041721046432655)
iranianBankCardCheck(6037997561388217)
iranianBankCardCheck(6362141802359837)
iranianBankCardCheck(6273811616832991)
iranianBankCardCheck(6037997561388217)
iranianBankCardCheck(6104337824093650)
iranianBankCardCheck(3982309207502750)
@MMnasrabadi
MMnasrabadi / UserDefaults+Codable+propertyWrapper.swift
Created August 26, 2020 12:59 — forked from quangDecember/UserDefaults+Codable+propertyWrapper.swift
property wrappers for saving to UserDefaults, generics or Codable
import Foundation
@propertyWrapper
struct UserDefault<T> {
let key: String
let defaultValue: T
init(_ key: String, defaultValue: T) {
self.key = key
@MMnasrabadi
MMnasrabadi / gist:c35337677c2d368dab967eeab685ff75
Created September 12, 2020 17:31 — forked from ebraminio/gist:5292017
Check Iranian National Code Validity - بررسی صحت کد ملی ایرانی - Clojure, C#, Ruby, JavaScript, Python, Scala, Java 8, PHP, C, Go, Swift, Kotlin, Groovy, Rust, Haskell, Erlang, Elixir
// Check Iranian National Code Validity - Clojure, C#, Ruby, JavaScript, Python, Scala, Java 8, PHP, C, Go, Swift, Kotlin, Groovy, Rust, Haskell, Erlang, Elixir
// بررسی صحت کد ملی ایران - کلوژر، سی‌شارپ، روبی، جاوااسکریپت، پایتون، اسکالا، جاوا ۸، پی‌اچ‌پی، سی، گو، سوئیفت، کاتلین، گرووی، راست، هسکل، ارلنگ، الکسیر
// در نسخه‌های قبل یکسان بودن اعداد نا معتبر تشخیص داده می‌شد ولی
// اعداد یکسان نامعتبر نیست http://www.fardanews.com/fa/news/127747
// بعضی از پیاده‌سازی‌ها سریع نیستند، می‌توانید نسخهٔ خود را بر پایهٔ
// نسخهٔ سی یا گو ایجاد کنید که بهترین سرعت را داشته باشد
/**
@MMnasrabadi
MMnasrabadi / simctl
Last active October 14, 2020 11:51
To open a custom URL scheme (or any website) in a simulator.
xcrun is a tool to run any tool inside Xcode from the command line. The tool for this job is simctl, which help you manage simulators.