SwiftMagicHelpers

main

Scalable helpers library for Swift
CodeNationDev/SwiftMagicHelpers
 __          _  __ _                      _                 _                     
/ _\_      _(_)/ _| |_  /\/\   __ _  __ _(_) ___  /\  /\___| |_ __   ___ _ __ ___ 
\ \\ \ /\ / / | |_| __|/    \ / _` |/ _` | |/ __|/ /_/ / _ \ | '_ \ / _ \ '__/ __|
_\ \\ V  V /| |  _| |_/ /\/\ \ (_| | (_| | | (__/ __  /  __/ | |_) |  __/ |  \__ \
\__/ \_/\_/ |_|_|  \__\/    \/\__,_|\__, |_|\___\/ /_/ \___|_| .__/ \___|_|  |___/
                                    |___/                    |_|                  

Package for unify most useful helpers for your day a day coding.

Main class

public struct HelperManager {}

Helpers

JSON

This helper provides methodf for encode and decode json to swift object and vice versa using Codable framework..

Interface:

Description : function for encode Swift object to json stirng.

  • Parameter result: Generic for pass object as argument for encode.
  • Throws: NSError with message and code Int with error for manage.
  • Returns: String: String format json as object encoded.
public static func jsonEncode<T: Codable>(_ result: T) throws -> String

Usage Example:

do {
let encoded = try HelperManager.JSON.jsonEncode(mockStructure)
} catch let error as NSError {
    //put here your error management.
}

Deocde:

Description: function for decode string json to swift object.

  • Parameters:
  • json: String json for decode.
  • type: type identity as baseinterface of json conversion.
  • Throws: NSError with message and code Int with error for manage.
  • Returns: T?: Type of class provided in the constructor for decode this json.
public static func jsonDecode<T: Codable>(_ json: String, type: T.Type) throws -> T 

Usage Example:

Object:

mockStruct = MockObject(field1: "value1", field2: 1, field3: ["arrayvalue1", "arrayvalue2"], field4: MockObject2(mField1: "mValue1", mField2: 2, mField3: [0, 1, 2, 3, 4, 5, 6]))

use

do {
let decoded = try HelperManager.JSON.jsonDecode(jsonString, type: MockObject.self)
} catch let error as NSError {
    //put here your error management.
}

UIApplication+TopViewController

This helper includes a fuction for catch top viewcontroller in Viewcycle stack

Interface:

Description : function for encode Swift object to json stirng.

  • Parameter base: Base of stack for find the top view controller.
  • Returns: UIViewController: The first view controller finded in base stack.
public static func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController?

Usage Example:

let topVC = HelperManager.UIApplicationHelpers.getTopViewController()

Meta

David Martin Saiz – @deividmarshalldavms81@gmail.com

Distributed under the MIT license. See LICENSE for more information.

https://github.com/CodeNationDev/

Version History

  • 1.0.0
    • JSON helper, encode and decode jsons.
    • UIapplicationHelper for get top viewcontroller.
    • UnitTesting
    • Readme documentation.

Description

  • Swift Tools 5.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Nov 07 2022 20:13:36 GMT-0500 (GMT-05:00)