Skip to content

Instantly share code, notes, and snippets.

View Marcocanc's full-sized avatar
🚀

Marco Cancellieri Marcocanc

🚀
View GitHub Profile
@Marcocanc
Marcocanc / UILabelCopyable.swift
Created December 19, 2017 09:33
UILabel subclass that supports Copying its contents to clipboard
import UIKit
class UILabelCopyable: UILabel {
override func copy(_ sender: Any?) {
UIPasteboard.general.string = text
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
@Marcocanc
Marcocanc / Array+filterKeyPath.swift
Created December 18, 2017 11:14
Filter with KeyPath
extension Array {
func filter<T: Equatable>(where keyPath: KeyPath<Element, T>, equals compareValue: T) -> [Element] {
return filter { $0[keyPath: keyPath] == compareValue }
}
}
@Marcocanc
Marcocanc / Array+sortedByKeyPath.swift
Last active December 18, 2017 11:06
Array sorting by keypath
extension Array {
func sorted<T: Comparable>(by keyPath: KeyPath<Element, T>, ordered areInIncreasingOrder: (T, T) -> Bool = (<) ) -> Array<Element> {
return self.sorted(by: {
areInIncreasingOrder($0[keyPath: keyPath], $1[keyPath: keyPath])
})
}
}
@Marcocanc
Marcocanc / Sequence+groupBy.swift
Created December 12, 2017 09:47
Swift 4 Keypath for grouping sequences
import Foundation
internal extension Sequence {
func grouped<T>(by keyPath: KeyPath<Element, T>) -> [T: [Element]] {
var groups = [T: [Element]]()
for element in self {
let key = element[keyPath: keyPath]
if !groups.keys.contains(key) {
groups[key] = [Element]()
}
@Marcocanc
Marcocanc / ripe.sh
Created July 10, 2017 07:15
Domain to Ripe Lookup
#!/bin/bash
IP=$(dig +short $1 | sed -n 1p)
whois -h whois.ripe.net -- "-T route ${IP}"
@Marcocanc
Marcocanc / UIFont+font.swift
Created June 15, 2017 12:23
Custom Fonts in iOS
import UIKit
extension UIFont {
/// Returns the font in the specified size and weight. Default weight is `regular`
open class func font(ofSize fontSize: CGFloat, weight: FontWeight = .regular) -> UIFont {
let fontName = "UniversNextPro-\(weight.rawValue)"
guard let font = UIFont(name: fontName, size: fontSize) else {
fatalError("\(fontName) not found")
}
return font
@Marcocanc
Marcocanc / FontWeight.swift
Last active June 15, 2017 12:23
Custom Fonts in iOS
import Foundation
/// Custom FontWeights to be used with the .font UIFont class functions
public enum FontWeight: String {
case regular = "Regular"
case medium = "Medium"
case black = "Black"
case light = "Light"
case thin = "Thin"
case bold = "Bold"
@Marcocanc
Marcocanc / scw-rclone-sync.sh
Last active December 31, 2021 11:48
Create a scaleway instance, put rclone and its config on it, sync drives and then terminate the the server
#!/bin/bash
set -e
SCALEWAY='scw --region="ams1"'
ARCH=X64
COMM_TYPE=$ARCH-2GB
IMAGE=xenial
TRANSFERS=7
TG_TOKEN=<BOT_TOKEN>
extension Collection {
public subscript(safe index: Index) -> Optional<Iterator.Element> {
guard self.startIndex..<self.endIndex ~= index else { return nil }
return self[index]
}
}
@Marcocanc
Marcocanc / UINavigationItem+Reactive.swift
Last active April 5, 2017 08:50
UINavigationItem+Reactive.swift
//
// UINavigationItem+Reactive.swift
// ReactiveQuotes
//
// Created by Marco Cancellieri on 14/01/2017.
// Copyright © 2017 Marco Cancellieri. All rights reserved.
//
import UIKit
import ReactiveSwift