SkillAgentSearch skills...

SwiftyUtils

All the reusable code that we need in each project

Install / Use

/learn @tbaranes/SwiftyUtils
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

SwiftyUtils

CI Status Language CocoaPods Compatible Carthage compatible Platform

SwiftyUtils groups all the reusable code that we need to ship in each project. This framework contains:

  • Extensions
  • Protocols
  • Structs
  • Subclasses

Working on iOS, macOS, tvOS, and watchOS, everything has been made to be easy to use! :tada:

Contents

Check out the repository to find examples / tests for each feature.

Swift, Foundation and CoreGraphics extensions:

SwiftUI:

SwiftUI Extension:

UIKit Extensions:

UIKit Protocols:

AppKit Extensions:

Protocols:

PropertyWrappers:

Others:

Swift, Foundation and CoreGraphics Extensions

Array extension

Safely access to an element:

var array = [1, 2, 3]
print(array[safe: 0]) // Optional(1)
print(array[safe: 10]) // nil

Find all the index of an object:

var array = [1, 2, 3, 1]
print(array.indexes(of: 1)) // [0,3]

Get index of first / last occurrence of an object:

var array = [1, 2, 3, 1]
print(array.firstIndex(of: 1)) // Optional(0)
print(array.lastIndex(of: 1)) // Optional(3)

Remove an object:

var array = [1, 2, 3]
myArray.remove(object: 2)
print(myArray) // [1, 3]
myArray.remove(objects: [1, 3])
print(myArray) // []

Remove all the duplicates:

var array = [0, 0, 1, 1, 2]
array.removeDuplicates()
print(array) // [0,1,2]

let array = [0, 0, 1, 1, 2]
let newArray.removedDuplicates()
print(newArray) // [0,1,2]

Remove all instances of an item:

var array = [0, 0, 1, 1, 2]
array.removeAll(0)
print(array) // [1,1,2]

let array = [0, 0, 1, 1, 2]
let newArray = array.removedAll(0)
print(newArray) // [1,1,2]

Check if an array is a subset of another array:

var array = [1, 2, 3]
print(array.contains([1, 2])) // true
print(array.contains([5])) // false

Determine if an array contains an object:

var array = [1, 2, 3]
print(array.contains(1)) // true
print(array.contains(11)) // false

Get intersection and union of two arrays:

var myArray = [1, 2, 3]
print(array.intersection(for: [1, 5, 3])) // [1, 3]
print(array.union(values: [5, 6])) // [1, 2, 3, 5, 6]

Get difference between two arrays:

var array = [1, 2, 3]
print(array.difference(with: [1])) // [2, 3]

Split into chunk of a specific size:

var array = [1, 2, 3, 4]
print(array.split(intoChunksOf: 2)) // [[1, 2], [3, 4]]

Bundle extension

Get bundle information:

Bundle.main.appName
Bundle(url: url)?.appName

Bundle.main.displayName
Bundle(url: url)?.displayName

Bundle.main.appVersion
Bundle(url: url)?.appVersion

Bundle.main.appBuild
Bundle(url: url)?.appBuild

Bundle.main.bundleId
Bundle(url: url)?.bundleId

Bundle.main.schemes
Bundle(url: url)?.schemes

Bundle.main.mainScheme
Bundle(url: url)?.mainScheme

Bundle.main.isInTestFlight
Bundle(url: url)?.isInTestFlight

CGFloat extension

Create a CGFloat from a Float or an Integer:

let imageViewTop = 15.f

CGPoint extension

Add two CGPoint:

var point1 = CGPoint(x: 10, y: 10)
let point2 = CGPoint(x: 10, y: 10)
print(point1 + point2) // CGPoint(x: 20, y: 20)

point1 += point2
print(point1) // CGPoint(x: 20, y: 20)

Substract two CGPoint:

var point1 = CGPoint(x: 10, y: 10)
let point2 = CGPoint(x: 10, y: 10)
print(point1 - point2) // CGPoint(x: 0, y: 0)

point1 -= point2
print(point1) // CGPoint(x: 0, y: 0)

Multiply a CGPoint with a scalar:

var point1 = CGPoint(x: 10, y: 10)
print(point1 * 2) // CGPoint(x: 20, y: 20)

point1 *= 2
print(point1) // CGPoint(x: 20, y: 20)

CGRect extension

Get the origin's x and y coordinates:

aRect.x // instead of aRect.origin.x
aRect.y // instead of aRect.origin.y

Change one property of a CGRect:

let rect = CGRect(x: 10, y: 20, width: 30, height: 40) 
let widerRect = rect.with(width: 100) // x: 10, y: 20, width: 100, height: 40
let tallerRect = rect.with(height: 100) // x: 10, y: 20, width: 30, height: 100
let rectAtAnotherPosition = rect.with(x: 100).with(y: 200) // x: 100, y: 200, width: 30, height: 40
let rectWithAnotherSize = rect.with(size: CGSize(width: 200, height: 200)) // x: 10, y: 20, width: 200, height: 200
let rectAtYetAnotherPosition = rect.with(origin: CGPoint(x: 100, y: 100)) // x: 100, y: 100, width: 30, height: 40

CGSize extension

Add two CGSize:

var size1 = CGSize(width: 10, height: 10)
let size2 = CGSize(width: 10, height: 10)
print(size1 + size2) // CGSize(width: 20, height: 20)

size1 += size2
print(size1) // CGSize(width: 20, height: 20)

Substract two CGSize:

var size1 = CGSize(width: 10, height: 10)
let size2 = CGSize(width: 10, height: 10)
print(size1 - size2) // CGSize(width: 0, height: 0)

size1 -= size2
print(size1) // CGSize(width: 0, height: 0)

Multiply a CGSize with a scalar:

var size1 = CGSize(x: 10, y: 10)
print(size1 * 2) // CGSize(width: 20, height: 20)

size1 *= 2
print(size1) // CGSize(width: 20, height: 20)

Color extension

Create colors with HEX values:

let myUIColor = UIColor(hex: "233C64") // Equals 35,60,100,1
let myNSColor = NSColor(hex: "233C64") // Equals 35,60,100,1

Access to individual color value:

let myColor = UIColor(red: 120, green: 205, blue: 44, alpha: 0.3)
print(myColor.redComponent) // 120
print(myColor.greenComponent) // 205
print(myColor.blueComponent) // 44
print(myColor.alpha) // 0.3

Get lighter or darker variants of colors instances:

let color = UIColor(red: 0.5, green: 0.5, blue: 1.0, alpha: 1.0)
let lighter = color.lighter(amount: 0.5)
let darker = color.darker(amount: 0.5)
// OR
let lighter = color.lighter()
let darker = color.darker()

let color = NSColor(red: 0.5, green: 0.5, blue: 1.0, alpha: 1.0)
let lighter = color.lighter(amount: 0.5)
let lighter = color.lighter()
// OR
let darker = color.darker(amount: 0.5)
let darker = color.darker()

Data Extension

Initialize from hex string:

let hexString = "6261736520313020697320736F2062617369632E206261736520313620697320776865726520697427732061742C20796F2E"
let data = Data(hexString: hexString)

Get hex string from data:

let data = Data(...)
let string = data.toHexString()
// string = "6261736520313020697320736F2062617369632E206261736520313620697320776865726520697427732061742C20796F2E" if using previous example value

Get UInt8 Array from data:

let data = Data(...)
let array = data.bytesArray

Map Data to Dictionary:

let dictionary = try data.toDictionary()

Date extension

Initialize from string:

let format = "yyyy/MM/dd"
let string = "2015/03/11"
print(Date(fromString: string, format: format)) // Optional("2015/03/11 00:00:00 +0000")

Convert date to string:

let now = Date()
print(now.string())
print(now.string(dateStyle: .medium, timeStyle: .medium))
print(now.string(format: "yyyy/MM/dd HH:mm:ss"))

See how much time passed:

let now = Date()
let later = Date(timeIntervalSinceNow: -100000)
print(later.days(since: now)) // 1.15740740782409
print(later.hours(since: now)) // 27.7777777733571
print(later.minutes(since: now)) // 1666.66666641732
print(later.seconds(since: now)) // 99999.999984026

Check if a date is in future or past:

let later = Date(timeIntervalSinceNow: -100000)
print(now.isInFutur

Related Skills

View on GitHub
GitHub Stars563
CategoryDevelopment
Updated1mo ago
Forks36

Languages

Swift

Security Score

100/100

Audited on Feb 2, 2026

No findings