Skip to content

Instantly share code, notes, and snippets.

@dnedrow
Last active June 3, 2020 15:03
Show Gist options
  • Select an option

  • Save dnedrow/de0eb8869e3909df595590a7e2404b47 to your computer and use it in GitHub Desktop.

Select an option

Save dnedrow/de0eb8869e3909df595590a7e2404b47 to your computer and use it in GitHub Desktop.
A swift extension on Bool that enables the use of Ints for boolean operations.
// Bool+Integer.swift
// Attribution 4.0 International (CC 4.0), see bottom for license details.
// https://gist.github.com/dnedrow/de0eb8869e3909df595590a7e2404b47
import Foundation
/// This extension enables the following behavior:
/// let x = Bool(5) // true
/// let y = Bool(0) // false
/// let z = Bool(-1) // false
extension Bool {
init(_ integer: Int){
// Remain consistent with Objective-C by counting 0 and <0 as false.
self.init(integer > 0)
}
}
// This work is licensed under Attribution 4.0 International (CC 4.0)
// You are free to:
// Share — copy and redistribute the material in any medium or format
// Adapt — remix, transform, and build upon the material
// for any purpose, even commercially.
//
// This license is acceptable for Free Cultural Works.
// The licensor cannot revoke these freedoms as long as you follow the license terms.
// See https://creativecommons.org/licenses/by/4.0/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment