Skip to content

Instantly share code, notes, and snippets.

View TsRebornz's full-sized avatar

Anton Makarenkov TsRebornz

  • Yerevan
View GitHub Profile
@TsRebornz
TsRebornz / UICollectionView + extension.swift
Created April 16, 2024 11:45
UICollectionView handy extensions
///
/// Allow you to register cells with code
/// <CellName>.reuseIdentifier
extension UICollectionReusableView {
static var reuseIdentifier: String {
return String(describing: Self.self)
}
}
///
import Foundation
final class MessagePrinter {
private let decoder = MessageDecoder()
func printEncodedMessage(_ encodedMessage: [Int]) {
decoder.decodeMessage(encodedMessage) { message in
self.printDecodedMessage(message)
}
}
struct Size {
let width: Int
let height: Int
}
struct Table {
let size: Size
let material: String
}
let matrixFirstCellValue = \Matrix[0,0]
struct Matrix {
var data: [[Int]]
subscript(row: Int, column: Int) -> Int? {
return data[column][row]
}
}
let array = [1,2,3,4,5]
let arrayFirstItemKeyPath = \[Int][0]
let firstArrayItem = array[keyPath: arrayFirstItemKeyPath] // -> 1
struct TableStructMapper<T> {
let sizeWidthdKeyPath: KeyPath<T, Int>
let sizeHeightKeyPath: KeyPath<T, Int>
let materialKeyPath: KeyPath<T, String>
func map(with mapper: T) -> Table {
return .init(
size: .init(
width: mapper[keyPath: sizeWidthdKeyPath],
height: mapper[keyPath: sizeHeightKeyPath]
struct TableDTO: Codable {
let width: Int
let height: Int
let material: String
enum CodingKeys: String, CodingKey {
case width
case height
case material
}
extension Sequence {
func sorted<T: Comparable>(by keyPath: KeyPath<Element, T>) -> [Element] {
return sorted {
$0[keyPath: keyPath] < $1[keyPath: keyPath]
}
}
}
let sortedTables = tables.sorted(by: \.size.width)
struct Size {
var width: Int
let height: Int
}
enum Material: String {
case wood
case iron
}
class Size { // Changed to class
var width: Int
let height: Int
init(width: Int, height: Int) {
self.width = width
self.height = height
}
}