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
@DanielStormApps
DanielStormApps / EmailValidator.swift
Last active March 16, 2021 14:22 — forked from darthpelo/EmailValidator.swift
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
extension String {
/// Checks if the `String` is a valid email address.
/// ````
/// // Example
/// "name@email.com".isValidEmailAddress() // true
/// "name(at)email(dot)com".isValidEmailAddress() // false
/// "name@email".isValidEmailAddress() // false
/// "name@.com".isValidEmailAddress() // false
/// "name.com".isValidEmailAddress() // false
// Swift's untyped errors are a goddam PiTA. Here's the pattern I use to try to work around this.
// The goal is basically to try to guarantee that every throwing function in the app throws an
// ApplicationError instead of some unknown error type. We can't actually enforce this statically
// But by following this convention we can simplify error handling
enum ApplicationError: Error, CustomStringConvertible {
// These are application-specific errors that may need special treatment
case specificError1
case specificError2(SomeType)
@JohnSundell
JohnSundell / OnboardingManager.swift
Last active May 21, 2020 08:19
An example of using #function for user defaults properties, and a test that guards against property name changes
import UIKit
class OnboardingManager {
private let userDefaults: UserDefaults
init(userDefaults: UserDefaults = .standard) {
self.userDefaults = userDefaults
}
func presentOnboardingControllerIfNeeded(in viewController: UIViewController) {
@ignazioc
ignazioc / autoclousure.swift
Created February 12, 2017 10:39
A simple explanation of the @autoclosure keyword
/*:
### Swift 3 @autoclosure
*/
import Cocoa
// Let's suppose this is complex funcion with lookup database etc.
func nextOrderNumber() -> Int {
return 1000
}
@rd13
rd13 / .swift
Last active September 11, 2022 17:00
Copy database file from bundle to documents in Swift 3
func copyDatabaseIfNeeded() {
// Move database file from bundle to documents folder
let fileManager = FileManager.default
let documentsUrl = fileManager.urls(for: .documentDirectory,
in: .userDomainMask)
guard documentsUrl.count != 0 else {
return // Could not find documents URL
@cabeca
cabeca / simulator_populator
Created September 23, 2014 21:30
This script removes and recreates all simulators in Xcode 6.
#!/usr/bin/env ruby
device_types_output = `xcrun simctl list devicetypes`
device_types = device_types_output.scan /(.*) \((.*)\)/
runtimes_output = `xcrun simctl list runtimes`
runtimes = runtimes_output.scan /(.*) \(.*\) \((com.apple[^)]+)\)$/
devices_output = `xcrun simctl list devices`
devices = devices_output.scan /\s\s\s\s(.*) \(([^)]+)\) (.*)/
@chriseidhof
chriseidhof / routes.swift
Created August 17, 2014 21:04
Type-safe routes in Swift
//
// main.swift
// Routes
//
// Created by Chris Eidhof on 17/08/14.
// Copyright (c) 2014 Chris Eidhof. All rights reserved.
//
import Foundation