Latest 0.2.2
Homepage https://github.com/bokhary3/SwiftyOptional
License MIT
Platforms ios 11
Authors

CI Status
Version
License
Platform

Using

SwiftyOtional will help you when using Optional types and have to work with ?? to get Wrapped value,
As example, rather than to get first item in optional array like that:

// Array
var countries: [String]? = ["Egypt", "Iraq", "Kwuit"]
let wrappedCountries: [String] = countries ?? []
let firstCountry = wrappedCountries.first ?? ""

You can get first item like that:

// Array
let countries: [String]? = ["Egypt", "Iraq", "Kwuit"]
let firstCountry = countries.swiftyArray.first.swiftyValue
print("First country: (firstCountry)")

Also with optional types like String, Int, Float, Double

// Optional types
let unwrappedString: String? = "Hello world!"
let unwrappedInt: Int? = 25
let unwrappedFLoat: Float? = 2.5
let unwrappedDouble: Double? = 100.6

// rather than using ??
print("""
Wrapped String: (unwrappedString ?? "")
Wrapped Int: (unwrappedInt ?? 0)
Wrapped Float: (unwrappedFLoat ?? 0.0)
Wrapped Double: (unwrappedDouble ?? 0.0)
""")

// you can get warpped value by using swiftyValue
print("""
Wrapped String: (unwrappedString.swiftyValue)
Wrapped Int: (unwrappedInt.swiftyValue)
Wrapped Float: (unwrappedFLoat.swiftyValue)
Wrapped Double: (unwrappedDouble.swiftyValue)
""")

// Also can set your default value when wrapped value is nil
print("""
Wrapped String: (unwrappedString.swiftyDefault(value: "non")), set non if unwrappedString is nil
Wrapped Int: (unwrappedInt.swiftyDefault(value: 1)), set 0 if unwrappedInt is nil
Wrapped Float: (unwrappedFLoat.swiftyDefault(value: 1.5)), set 1.5 if unwrappedFLoat is nil
Wrapped Double: (unwrappedDouble.swiftyDefault(value: 5.5)), set 5.5 if unwrappedDouble is nil
""")

// Also you can parse optional Any and get wrapped value
let username: Any? = "Bokhary"
let itemsOrderCount: Any? = 10
let totalPrice: Any? = 500.5

print("""
Parse Any to Wrapped String: (username.swiftyString)
Parse Any to Wrapped Int: (itemsOrderCount.swiftyInt)
Parse Any to Wrapped Double: (totalPrice.swiftyDouble)
""")

// Can parse between types
let tenTimes: String? = "10"
let feeCost: Int? = 200
print("""
Parse Optioanl String to Wrapped Int: (tenTimes.intValue)
Parse Optional Int to Wrapped Double: (feeCost.doubleValue)
""")

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Swift4.2, Xcode 10 and later.

Installation

SwiftyOptional is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'SwiftyOptional'

Author

bokhary3, [email protected]

License

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

Latest podspec

{
    "name": "SwiftyOptional",
    "version": "0.2.2",
    "summary": "SwiftyOptional is collection of handy extensions to make optional life easy.",
    "homepage": "https://github.com/bokhary3/SwiftyOptional",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "bokhary3": "[email protected]"
    },
    "source": {
        "git": "https://github.com/bokhary3/SwiftyOptional.git",
        "tag": "0.2.2"
    },
    "social_media_url": "https://twitter.com/bokhary92",
    "platforms": {
        "ios": "11"
    },
    "swift_version": "4.2",
    "source_files": "SwiftyOptional/Classes/*"
}

Pin It on Pinterest

Share This