Skip to content

Instantly share code, notes, and snippets.

View Marcocanc's full-sized avatar
🚀

Marco Cancellieri Marcocanc

🚀
View GitHub Profile
@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 / 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 / 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 / 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 / HomeKitQR.swift
Created January 3, 2018 09:38
Reverse engineering HomeKit QR Code decoding/encoding
import Foundation
func setupCodeFrom(uri: String) -> String {
let startIndex = uri.index(uri.startIndex, offsetBy: "X-HM://".count)
let endIndex = uri.index(uri.endIndex, offsetBy: -4)
var actualString = uri[startIndex..<endIndex]
let number = UInt(actualString, radix: 36)!
var code = String(format: "%08u", number & 0x7ffffff)
@Marcocanc
Marcocanc / Label.swift
Last active September 3, 2023 09:14
A UILabel subclass that can hold attributes and apply them to text
import UIKit
/// A UILabel subclass that can hold attributes and apply them to text
@IBDesignable
final class Label: UILabel {
convenience init(attributes: [NSAttributedStringKey: Any]) {
self.init()
self.attributes = attributes
}
// MARK: Properties
@Marcocanc
Marcocanc / UILayoutPriority+offset.swift
Created January 26, 2018 12:20
UILayoutPriority Offset
extension UILayoutPriority {
/// Returns a UILayoutPriority that offsets an existing one by the specified value.
static func offset(_ prio: UILayoutPriority, by offset: Float) -> UILayoutPriority {
return UILayoutPriority(rawValue: prio.rawValue + offset)
}
}
@Marcocanc
Marcocanc / Equatable+CheckIfEqualTo.swift
Created January 27, 2018 12:24
Easily compare two objects by using KeyPath
extension Equatable {
func checkIfEqualTo<T: Equatable>(_ other: Self, byComparing paths: KeyPath<Self,T>...) -> Bool {
for keyPath in paths {
guard self[keyPath: keyPath] == other[keyPath: keyPath] else {
return false
}
}
return true
}
}
@Marcocanc
Marcocanc / Signal+InjectError.swift
Created January 29, 2018 10:46
Inject an Error on a given Signal
import ReactiveSwift
import Result
extension Signal {
func injectError(_ error: Error, on signal: Signal<(), NoError>) -> Signal<Value, Error> {
return Signal { observer, lifetime in
lifetime += signal.observe { event in
if event.value != nil {
observer.send(error: error)
@Marcocanc
Marcocanc / String+StringFromDelta.swift
Last active April 3, 2018 16:13
Apply DiffMatchPatch Diffs to a String in pure Swift
import Foundation
extension String {
func string(withDelta deltaString: String) throws -> String {
var outString = String()
var indexPointer = self.startIndex
let inStringCount = self.utf16.count
try deltaString.components(separatedBy: "\t").forEach { string in
guard let firstChar = string.first else { return }
let param = string[string.index(after: string.startIndex)...]