Skip to content

Art3mS1d/EasySwift

Repository files navigation

EasySwift

Collection of Swift tweaks for iOS.

Mutation Operator

Example:

static let durationFormatter = DateComponentsFormatter() ~ {
    $0.allowedUnits = .minute
    $0.calendar = .current ~ { $0.locale = preferredLocale }
    $0.unitsStyle = .full
}

Same as:

static let durationFormatter: DateComponentsFormatter = {
    let formatter = DateComponentsFormatter()
    formatter.allowedUnits = .minute
    var calendar = Calendar.current
    calendar.locale = preferredLocale
    formatter.calendar = calendar
    formatter.unitsStyle = .full
    return formatter
}()

Sequence + KeyPath

Example:

let filenames = resources.flatMap(\.items).map(\.filename)
let sorted = videos.sorted(by: \.resolution)
let total = files.sum(\.size)

Same as:

let filenames = resources.flatMap { $0.items }.map { $0.filename }
let sorted = videos.sorted { $0.resolution < $1.resolution }
let total = files.reduce(0) { $0 + $1.size }

Observable

class Model {

    @Observable
    var searchText = ""
}

// to store subscription
var searchObservation: Observation?

// to subscribe
model.$searchText { text in
    ...
}.dispose(in: &searchObservation)

// to send
model.searchText = "hi"

// to unsubscribe
searchObservation = nil

// without storing state
let onComplete = Observer<Bool>()

// to send
onComplete(true)

CoreData

// Query objects of type File where filename are in list and sorted by size
let files = Query<File>.all(\.filename ~= filenames, by: \.size, in: context)

// Query recently created File
let recent = Query<File>.any(by: \.createdAt, desc: true, in: context)

// Delete entries where object downloaded and size above 10 MB
Query<File>.delete(\.downloaded == true && \.size > 10.MB, in: context)

Basic Metrics

URLCache(memoryCapacity: 1.MB, diskCapacity: 20.MB, directory: url)
Timer.scheduledTimer(withTimeInterval: 5.minutes, repeats: true) { ...
if changeDate < Date() - 2.hours { ...

UIColor + Hex

// To initialise with hex:
UIColor("#33ff55")
// or:
UIColor(0x33ff55)

// To get hex from color:
let hex = color.hexString

UICollectionView + Type

collectionView.register(FontCell.self)
let cell = collectionView.dequeue(for: indexPath) as FontCell

Same as:

collectionView.register(FontCell.self, forCellWithReuseIdentifier: "FontCell")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FontCell", for: indexPath) as! FontCell

About

Collection of Swift tweaks for iOS.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages