Giter Site home page Giter Site logo

optionalextensions's Introduction

ExtOptional

Build Status codecov GitHub issues

ios GitHub license Swift 3.0.x ios Twitter

Handful of functions, operators and properties that will help you work with Optional types.

Usage

Call require() to require Optional values to be non-nil, or throws fatalError with optionally given hint for debugging purposes:

let gitURL: URL? = URL(string: "https://github.com/Smitters/OptionalExtensions")
let request = URLRequest(url: gitURL.require(hint: "Invalid URL"))

Call unwrap(default: value) to return the contained value or a default:

let daysInMonth: Int? = nil
let daysCount = daysInMonth.unwrap(default: 31) // returns 31

Property isNone returns true if the Optional is nil:

let x: String? = nil
let r = x.isNone // r == true

Property isSome returns true if the Optional is .some:

let x: String? = "foo"
let r = x.isSome // r == true

Property stringRepresentation ensures that the text you set never ever includes that annoying additional “Optional(…)”:

let x: String? = "String"
let y: Int? = 31
let z: String? = nil

print("\(x) \(y) \(z)") // Optional("String") Optional(31) nil
print("\(x.stringRepresentation) \(y.stringRepresentation) \(z.stringRepresentation)") // String 31

Swift 3 Removes Optional Comparison Operators, but you can achieve same behaviour adding ? symbol to Swift's standart comparison operator:

let x: Int? = 43
let y: Int? = 5
let z: Int? = nil

x >=? y // true
x >? z // true
z >? x // false
y <? x // true
x <=? 43 // true
x <=? y //false

let array: [Int?] = [2, 4, nil, 1, 5, nil, 3]
let sortedArray = array.sorted(by: >?) // [5, 4, 3, 2, 1, nil, nil]

Installation

CocoaPods:

Add pod "ExtOptional" to your Podfile.

Manual:

Clone the repo and drag the file OptionalExtensions.swift into your Xcode project.

Help, feedback or suggestions?

  • Open an issue if you need help, if you found a bug, or if you want to discuss a feature request.
  • Open a PR if you want to make some change to ExtOptional.
  • Contact @smetankin93 on Twitter for discussions, news & announcements about new pods.

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.