Skip to content

Instantly share code, notes, and snippets.

View lucaswkuipers's full-sized avatar
💻
iOS Development 

Lucas Werner Kuipers lucaswkuipers

💻
iOS Development 
View GitHub Profile
@lucaswkuipers
lucaswkuipers / Cell+dequeue.swift
Last active March 29, 2024 01:33
UIKit Dequeue cell extensions
import UIKit
extension UITableView {
func dequeue<Cell: UITableViewCell>(
_ reusableCellType: Cell.Type,
for indexPath: IndexPath,
identifier: String? = nil
) -> Cell {
let identifier = identifier ?? String(describing: reusableCellType)
register(reusableCellType, forCellReuseIdentifier: identifier)
@lucaswkuipers
lucaswkuipers / NonEmptyArray<Element>+mean.swift
Created February 25, 2024 11:02
Type Driven Design Comparison
// LinkedIn Post: https://www.linkedin.com/posts/lucaswk_creating-rule-enforced-types-is-a-powerful-activity-7167473213868785665-tJRS?utm_source=share&utm_medium=member_desktop
// Ordinary way
extension [Double] {
var mean: Double? {
guard !isEmpty else {
return nil
}
guard let sum else {
@lucaswkuipers
lucaswkuipers / XCTestCase+arbitrary.swift
Created February 22, 2024 07:00
XCTestCase extension to express when a value is irrelevant to the test outcome.
import XCTest
/// An extension for `XCTestCase` to express when a value is not relevant to the test outcome.
///
/// By using the `arbitrary` method, you can indicate that a certain value provided to the test does not play a significant role in the test scenario. It helps to avoid the creation of distracting variables whose values are inconsequential, focusing on what really matters in the test. The term 'arbitrary' is chosen to signify that the value is placeholder and could be substituted for any other value without affecting the test's behavior.
///
/// ## Topics
///
/// - Reducing Noise in Tests
///
@lucaswkuipers
lucaswkuipers / Color+rgba.swift
Created January 19, 2024 17:52
RGBA components from SwiftUI.Color
import SwiftUI
#if canImport(UIKit)
typealias PlatformColor = UIColor
#elseif canImport(AppKit)
typealias PlatformColor = NSColor
#endif
extension Color {
var rgba: (red: Double, green: Double, blue: Double, alpha: Double) {
var red = CGFloat()
var green = CGFloat()
@lucaswkuipers
lucaswkuipers / ProfileView.swift
Last active February 26, 2024 10:58
LinkedIn Profile View in SwiftUI
import SwiftUI
struct CustomButton {
let title: String
let url: URL
}
struct Profile {
let name: String
let headline: String
@lucaswkuipers
lucaswkuipers / Color+lerp.swift
Last active January 10, 2024 04:49
Color Helpers iOS
extension Double {
func interpolated(to b: Double, at t: Double) -> Double {
self + (b - self) * (t * t)
}
}
extension [Color] {
func rgbaLerp(at t: Double) -> Color {
let position = t * Double(count - 1)
let i = Int(position)
@lucaswkuipers
lucaswkuipers / Color+rgbaLerp.swift
Created January 9, 2024 15:36
Color linear interpolation in Swift (and rgba components extension)
import SwiftUI
// MARK: - Lerp
extension Color {
func rgbaLerp(to endColor: Color, _ t: CGFloat) -> Color {
let a = rgba
let b = endColor.rgba
let red = a.r + (b.r - a.r) * t
@lucaswkuipers
lucaswkuipers / ArrayCountPerformanceTests.swift
Created December 26, 2023 06:13
Swift Array<T> count performance test comparison
import XCTest
// # Setup
// Big array size: 1_000_000_000 (1 billion elements of 12345)
// Small array size: 2 [12345, 12345]
// Loop: 1_000_000 (1 million) times.
// Machine: Macbook Pro 2023 16" M2 Max 32gb
// # Results
// TLDR: Stored: 3% faster
@lucaswkuipers
lucaswkuipers / Collection<Element: Numeric>+sum.swift
Created December 24, 2023 22:55
Sum extension to numeric collections
extension Collection where Element: Numeric {
var sum: Element {
reduce(0, +)
}
}
@lucaswkuipers
lucaswkuipers / Color+hexLiteral.swift
Created December 24, 2023 06:01
Color from Hex literal
import SwiftUI
extension Color: ExpressibleByIntegerLiteral {
public init(integerLiteral value: UInt32) {
let red = Double((value & 0xFF0000) >> 16) / 255
let green = Double((value & 0x00FF00) >> 8) / 255
let blue = Double(value & 0x0000FF) / 255
self.init(red: red, green: green, blue: blue)
}
}