Skip to content

Instantly share code, notes, and snippets.

View darthpelo's full-sized avatar
🏠
Working from home

Alessio Roberto darthpelo

🏠
Working from home
  • Mobiquity Inc.
  • Amsterdam, Noord-Holland
View GitHub Profile
@darthpelo
darthpelo / EmailValidated.swift
Last active February 2, 2020 14:24
An example regarding how Swift 5.1 Property Wrapper can be useful for a light Input Validation enforcement.
@propertyWrapper
struct EmailValidated {
private(set) var defaultValue: String = ""
var wrappedValue: String {
get { defaultValue }
set { defaultValue = maxLength(newValue) && isValid(newValue)
?
newValue
:
@darthpelo
darthpelo / Email.swift
Last active July 30, 2019 20:26
Improve input validation considering the input as a Type
import Foundation
struct Email {
private var string: String
init(_ string: String) throws {
try Validations.email(string)
self.string = string
}
func application(_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
return handle(userAcitivity: userActivity)
}
// MARK: - Private
private func handle(userAcitivity activity: NSUserActivity) -> Bool {
switch activity.activityType {
case "com.mobiquity.TakeUrPill.history":
final class HistoryViewController: BaseViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let information = presenter?.information {
activitySetup(information)
}
}
}
import Foundation
import Intents
struct SiriService {
struct ActivityInformation {
let activityType: String
let activityTitle: String
let activitySuggestedInvocation: String
}
@darthpelo
darthpelo / swiftlint.yml
Created March 2, 2018 09:15
Basic aggressive SwiftLint
excluded: # paths to ignore during linting. Takes precedence over `included`.
- Tests
- Pods
- Carthage
- R.generated.swift
- MyPlayground.playground
disabled_rules:
- trailing_whitespace
- multiple_closures_trailing_closure
@darthpelo
darthpelo / watchType.swift
Created December 7, 2017 10:15
How to determinate Apple Watch model at runtime.
import Foundation
import UIKit // Only to use CGFloat 😬
import WatchKit
enum WatchModelType {
case large
case small
}
struct WatchModel {
@darthpelo
darthpelo / DynamicBinding.swift
Last active August 9, 2022 07:56
ViewModel, binding and generic
// Ispired by http://rasic.info/bindings-generics-swift-and-mvvm/
class Dynamic<T> {
typealias Listener = (T) -> Void
var listener: Listener?
func bind(listener: Listener?) {
self.listener = listener
}
func bindAndFire(listener: Listener?) {
@darthpelo
darthpelo / ClassCheck.swift
Created December 21, 2016 14:20
Class check in Swift
class foo {
let className = "foo"
}
let a = foo()
func isFoo(_ instance: Any) -> Bool {
return instance is foo
}
@darthpelo
darthpelo / EmailValidator.swift
Last active September 25, 2020 08:51
This regular expression is adapted from a version at regular-expressions.info and is a complete verification of RFC 2822. Source: http://www.cocoawithlove.com/2009/06/verifying-that-string-is-email-address.html. Dedicate article: https://medium.com/@darthpelo/email-validation-in-swift-3-0-acfebe4d879a
/// Validate email string
///
/// - parameter email: A String that rappresent an email address
///
/// - returns: A Boolean value indicating whether an email is valid.
func isValid(_ email: String) -> Bool {
let emailRegEx = "(?:[a-zA-Z0-9!#$%\\&‘*+/=?\\^_`{|}~-]+(?:\\.[a-zA-Z0-9!#$%\\&'*+/=?\\^_`{|}” +
“~-]+)*|\“(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\” +
“x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\“)@(?:(?:[a-z0-9](?:[a-” +
“z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5" +