Giter VIP home page Giter VIP logo

swifthelpers's Introduction

SwiftHelpers

Swift

SwiftHelpers is a collection of convenient Swift extensions and helper functions designed to simplify common tasks and improve code readability in your Swift projects. These utilities cover a wide range of functionalities, from working with arrays and strings to manipulating dates and handling optional values.

Features

  • Array extensions for sorting, subscripting, and mutation
  • String extensions for regex evaluation and validation
  • Date extensions for easy date manipulation and formatting
  • Optional extensions for error handling and unwrapping
  • Codable extensions for easy JSON encoding and decoding
  • Sequence extensions for unique filtering and transformations
  • Bundle extensions for retrieving app version and build information
  • Comparable extensions for value clamping

Installation

Swift Package Manager

Add the following line to the dependencies in your Package.swift file:

swift

.package(url: "https://github.com/stalkermv/SwiftHelpers", from: "1.0.0")

Then, add SwiftHelpers to your target's dependencies:

swift

.target(name: "YourTarget", dependencies: ["SwiftHelpers"])

Usage

After installing the library, simply import SwiftHelpers at the top of your Swift files and start using the provided extensions and helper functions.

import SwiftHelpers

// Example usage
let numbers = [1, 3, 2, 4]
let sortedNumbers = numbers.sorted { $0 < $1 }

let dateString = "2023-01-01T12:00:00Z"
let date = try? Date(iso8601: dateString)

Storage

The library defines two protocols for synchronous and asynchronous storage objects, respectively:

  • Storage: A synchronous storage object that defines a set of methods for saving, loading, and removing encoded objects from a storage object.
  • AsyncStorage: An asynchronous storage object that defines a set of methods for saving, loading, and removing encoded objects from a storage object asynchronously.

The Storage target provides two default implementations of Storage:

  • KeychainStorage: A synchronous storage object that uses the system keychain to store and retrieve data.
  • UserDefaults: A synchronous storage object that uses the UserDefaults system to store and retrieve data.

The library provides a property wrapper StorableValue that enables easy and safe storage of any Codable object. To use StorableValue, initialize it with the default value, a key, and a storage object.

import SwiftHelpers
import Storage

// Example usage of StorableValue with UserDefaults
@StorableValue(key: "exampleKey", storage: .userDefaults)
var exampleValue: String = "default"

// Example usage of StorableValue with KeychainStorage
@StorableValue(key: "exampleKey", storage: .userDefaults)
var secureValue: ExampleStruct? = nil

enum ExampleEnum: String, Codable {
    case firstCase
    case secondCase
}

struct ExampleStruct: Codable {
    var intValue: Int
    var stringValue: String
    var enumValue: ExampleEnum
}

secureValue = ExampleStruct(intValue: 123, stringValue: "example", enumValue: .firstCase)

Contributing

Contributions to SwiftHelpers are welcome! Feel free to submit a pull request with new features, improvements, or bug fixes. Please make sure to add documentation and unit tests for any changes you make.

License

SwiftHelpers is released under the MIT License. See the LICENSE file for more information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.