Swift Useful Extensions

Table of Contents


Date Extensions

Formatting date to localized strings using Template and styles.

Example Usage

let date = Date()
let dateFormatter = DateFormatter()

var template = "yMMM"
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: template, options: 0, locale: Locale(identifier: "en_US"))
print(dateFormatter.string(from: date)) //Apr 2024

dateFormatter.dateStyle = .full
dateFormatter.timeStyle = .full
dateFormatter.locale = Locale(identifier: "ja_JP")
print(dateFormatter.string(from: date)) //2024年4月16日 火曜日 10時20分14秒 日本標準時

Date Demo

For further details, check out Swift: Working with Date.

Color Extensions

From/To rgb, hex Int, and hex string, and calculate inverted color.

Example Usage

let colorString = "#0000FF"
let color = UIColor(hex: Self.colorString) ?? .clear
let rgba = color.rgba ?? ""
let hex = color.hex(includeAlpha=true) ?? ""

Color Demo

For further details, check out Swift: Working with Colors.

Path Extensions

Retrive startpoint, element count of a Path. Get all points and tangents on a Path.

Example Usage

let path = Path { path in
    path.move(to: CGPoint(x: 100, y: 200))
    path.addQuadCurve(to: CGPoint(x: 90, y: 400), control: CGPoint(x: 500, y: 130))
    path.addLine(to: CGPoint(x: 200, y: 500))
    path.addCurve(to: CGPoint(x: 300, y: 100), control1: CGPoint(x: 50, y: 100), control2: CGPoint(x: 180, y: 300))
        path.closeSubpath()
}
let points = path.points(pointsPerElement: 10)
let pointsWithTangents = path.pointsWithTangents(pointsPerElement: 10)

Path Animation Demo

For further details, check out