FHExtensions

1.6.2

Some usefull Foundation and UIKit Extensions.
FelixHerrmann/FHExtensions

What's New

1.6.2

2020-09-26T21:14:09Z

String.capitalizedFirst

FHExtensions

GitHub version GitHub license Twitter

Some usefull Foundation and UIKit Extensions.

Will be expanded over time.

Requirements

  • macOS 10.10+
  • iOS 9.0+
  • tvOS 9.0+

Installation

Swift Package Manager

Add the following to the dependencies of your Package.swift:

.package(url: "https://github.com/FelixHerrmann/FHExtensions.git", from: "x.x.x")

Manual

Download the files in the Sources folder and drag them into you project.

Usage

Array

subscript(safe index: Index) -> Element?

This subscript checks, if the index is in range.


Getting array values works like that:

let array = [0, 1, 2]

print(array[1]) // 1
print(array[safe: 1]) // Optional(1)

print(array[3]) // Fatal error: Index out of range
print(array[safe: 3]) // nil

Setting array values works also safely:

var array = [0, 1, 2]

array[safe: 2] = 3
print(array) // [0, 1, 3]

array[safe: 3] = 4
print(array) // [0, 1, 3]

CGRect

Coordinates: x, y, top, bottom, left, right, midX, midY, center

Convenience properties for CGRect coordinates.

These properties also contains setters which will recreate the frame entirely.

Date

init?(_:_:_:hour:minute:second)

This initializer can create a Date object by date components. It can fail if a date could not be found which matches the components.

let date = Date(23, 2, 1999)
let dateWithTime = Date(23, 2, 1999, hour: 9, minute: 41, second: 0)

The time values are optional.

JSONDecoder

DateDecodingStrategy.iso8601withFractionalSeconds

An ISO 8601 DateDecodingStrategy with fractional seconds. Something like 1999-02-23T08:41:00.000Z will work with the decoder.

JSONEncoder

DateEncodingStrategy.iso8601withFractionalSeconds

An ISO 8601 DateEncodingStrategy with fractional seconds. Something like 1999-02-23T08:41:00.000Z will be the output from the encoder.

String

capitalizedFirst

A copy of the string where the first letter is capitalized.

UIColor

RGB: red, green, blue, alpha

These properties are based on the getRed(_:green:blue:alpha) method.

init?(hex:)

This initializer can create a UIColor object by a hex string. It can fail if the string has not the correct format (RGBA).

let yellow = UIColor(hex: "#ffff00ff")

UIDevice

modelIdentifier

With UIDevice.current.modelIdentifier you are able to get the model identifier of the current device as String.

This works also on Mac (Catalyst).

UIDirectionalPanGestureRecognizer

A concrete subclass of UIPanGestureRecognizer that looks for panning (dragging) gestures in the setted direction.

let directionalPanRecognizer = UIDirectionalPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
directionalPanRecognizer.direction = .vertical
view.addGestureRecognizer(directionalPanRecognizer)

UserDefault

A property wrapper which stores the wrapped value in the UserDefaults.

@UserDefault("test", defaultValue: "") var test: String

The wrapped value must be of type UserDefaultType. For every other type use the CodableUserDefault wrapper.

OptionalUserDefault

The UserDefault property wrapper but for optional types.

@OptionalUserDefault("test") var test: String?

CodableUserDefault

This property wrapper works exactly like the UserDefault one but accepts any type that conforms to the Codable protocol.

struct TestType: Codable {
    let name: String
}

@CodableUserDefault("test", defaultValue: TestType(name: "") var test: TestType

License

FHExtensions is available under the MIT license. See the LICENSE file for more info.

Description

  • Swift Tools 5.3.0

Dependencies

  • None
Last updated: Wed Nov 25 2020 12:28:52 GMT-0500 (GMT-05:00)