Latest 1.0.0
Homepage https://github.com/OlegTsib/OTUnwrapper
License MIT
Platforms ios 8.0
Authors

OTUnwrapper

Unwrap Optional and JSON values

VersionAuthorSwiftSwift

Table of Contents

  1. Requirements
  2. Installation
  3. Opportunities
  4. How to Use
  5. Unwrap Optional Value
  6. Unwrap Data to JSON
  7. Unwrap JSON value
  8. Unwrap JSON object

Requirements:

  • iOS 8.0+
  • Xcode 9.4+
  • Swift 4.2+

Installation:

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate GAInfiniteCollectionKit-iOS into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
pod 'OTUnwrapper'
end

Then, run the following command:

$ pod install

Opportunities:

  1. Unwrap Optional
  2. Unwrap data to JSON
  3. Unwrap JSON value
  4. Unwrap JSON to Object
  5. Unwrap with default value

How to Use:

Import:

import OTUnwrapper

Unwrap Optional Value:

You have two ways to unwrap optional

Without default value:

If you do not use default value you need to specify value type

var optionalString   : String?
var optionalString2 : String?

optionalString2 = "String is not nil"
let value   : String = optionalString.unwrap()  //  value   = "" 
let value2 : String = optionalString.unwrap()  //  value2 = "String is not nil" 

With default value:

If you use default value you do not need to specify value type

var optionalString   : String?
var optionalString2 : String?

optionalString2 = "String is not nil"
let value   = optionalString.unwrap("Value is nil")    // value   = "Value is nil"
let value2 = optionalString2.unwrap("Value is nil")  //  value2 = "String is not nil"

Unwrap Data to JSON

Example:

let data = [DATA]
let json = data.unwrapToJSON()

Unwrap JSON value

Example:

let json: [String:Any] = ["name": "OTUnwrapper", "URL" : "https://github.com/OlegTsib/OTUnwrapper","liked": true]

let name: String = json.unwrapValue("name")        // name = "OTUnwrapper"
let liked            = json.unwrapValue("liked",false) // liked   = true

Unwrap JSON object

First step:

Create Class or Struct and implement protocol OTUnwrappedObject

Example:

Struct User:

import OTUnwrapper

struct User: OTUnwrappedObject
{
let name       : String
let imageURI : String

init(dictionary: [String : Any])
{
email       = dictionary.unwrapValue("name")
imageURI = dictionary.unwrapValue("image")
}
}

JSON File:

{
"user":
{
"name ": "OTUnwrapper",
"image": "https://avatars0.githubusercontent.com/u/15246986?s=400&v=4"
}
}

Unwrap to object:

guard let path = Bundle.main.path(forResource: "FileName", ofType: "json") else { return }
do
{
let data          =  try Data(contentsOf: URL(fileURLWithPath: path))
let json          =  data.unwrapToJSON()
let user: User  =  json.unwrapToObject("user")
}
catch
{
}

Author

OlegTsib, [email protected]

License

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

Latest podspec

{
    "name": "OTUnwrapper",
    "version": "1.0.0",
    "summary": "Unwrap Optional and JSON values",
    "description": "TODO: Add long description of the pod here.",
    "homepage": "https://github.com/OlegTsib/OTUnwrapper",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "OlegTsib": "[email protected]"
    },
    "source": {
        "git": "https://github.com/OlegTsib/OTUnwrapper.git",
        "tag": "1.0.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "OTUnwrapper/Classes/**/*"
}

Pin It on Pinterest

Share This