2025-08-28

Filter unique elements from a Sequence

extension Sequence where Element: Hashable {
    func unique() -> [Element] {
        var seen: Set<Element> = []
        return filter { seen.insert($0).inserted }
    }
}

2025-08-26

Masking in SwiftUI

LinearGradient(
    gradient: Gradient(colors: [.pink, .indigo]),
    startPoint: .leading,
    endPoint: .trailing
)
.frame(width: 70, height: 70)
.mask {
    Image(systemName: "circle.hexagonpath")
    .resizable()
    .frame(width: 70, height: 70)
}

Result:Link icon

masking


2025-01-01

Adding/Subtracting from a Date

extension Date {
    /// Adds the specified time components to this date
    func add(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -> Date? {
        let components = DateComponents(year: years, month: months, day: days, hour: hours, minute: minutes, second: seconds)
        return Calendar.current.date(byAdding: components, to: self)
    }

    /// Subtracts the specified components from this date
    func subtract(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -> Date? {
        return add(years: -years, months: -months, days: -days, hours: -hours, minutes: -minutes, seconds: -seconds)
    }
}

Usage:

// Add one year and a half to the current Date: 
Date().add(years: 1, months: 6)

// Add 30 seconds to the current Date:
Date().add(seconds: 30)

// Substract 10 minutes from now:
Date().add(minutes: -10)
// or
Date().sustract(minutes: 10)

2025-01-01

Simple ScrollView Wrapper

extension View {
  func scrollable() -> some View {
    ScrollView {
      self
    }
  }
}

Usage:

SomeView {
  // ...
}
.scrollable()

2025-01-01

Swift Package dependencies declaration

dependencies: [
.package(url: "https://github.com/author/repo-name.git", from: "x.x.x"),
],
targets: [
	.target(
		name: "MyCurrentApp",
		dependencies: [
    
      // `name` = Packages.swift > products > .library > name > "LibraryName"
      // `package` = package name in url `/repo-name.git`
			.product(name: "LibraryName", package: "repo-name"),
      
		],
	)
]

2024-12-01

HTML Favicon sizes

<!-- For all browsers -->
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">

<!-- For Google and Android -->
<link rel="icon" type="image/png" sizes="48x48" href="favicon-48x48.png">
<link rel="icon" type="image/png" sizes="192x192" href="favicon-192x192.png">

<!-- For iPad -->
<link rel="apple-touch-icon" type="image/png" sizes="167x167" href="favicon-167x167.png">
<!-- For iPhone -->
<link rel="apple-touch-icon" type="image/png" sizes="180x180" href="favicon-180x180.png">

Source