Skip to content

Instantly share code, notes, and snippets.

import Foundation
/// 9 radom numbers followed by two verifying digits
var randomValidCpf: String {
var cpf = (1...11).map { _ in Int(arc4random_uniform(9)) }
cpf[9] = calcVerifyingDigit(originalValue: cpf, weights: (2...10).map{$0}.reversed())
cpf[10] = calcVerifyingDigit(originalValue: cpf, weights: (2...11).map{$0}.reversed())
@danielCarlosCE
danielCarlosCE / ChainResponsibility.playground.swift
Created October 13, 2017 17:42
Chain of Responsibility to handle errors on iOS application
import UIKit
class AppDelegate {
var window: UIWindow?
}
//Chain of Resposability: Handler
protocol ErrorHandler {
var errorHandlerSuccessor: ErrorHandler? {get}
func handleError(error: Error)
override init() {
super.init()
//Make sure the device can handle video
guard let videoDevice = AVCaptureDevice.default(for: .video),
let deviceInput = try? AVCaptureDeviceInput(device: videoDevice) else {
return
}
//session
extension AVCodeReader: AVCaptureMetadataOutputObjectsDelegate {
func metadataOutput(_ captureOutput: AVCaptureMetadataOutput,
didOutput metadataObjects: [AVMetadataObject],
from connection: AVCaptureConnection) {
guard let readableCode = metadataObjects.first as? AVMetadataMachineReadableCodeObject,
let code = readableCode.stringValue else {
return
}
//Vibrate the phone
@danielCarlosCE
danielCarlosCE / ContentSizePagingCollectionDelegate.swift
Last active June 28, 2022 15:40
Horizontal paging for collections based on the content size
//
// ContentSizePagingCollectionDelegate.swift
// CardsCarousel
//
// Created by Daniel Carlos Souza Carvalho on 2/20/21.
//
import UIKit
/// Horizontal paging for collections based on the content size