# String to Bool

**URL:** <https://forums.swift.org/t/string-to-bool/32633>\
**Category:** Using Swift\
**Tags:** foundation\
**Created:** [January 16, 2020, 12:23am UTC](https://forums.swift.org/t/string-to-bool/32633 "2020-01-16T00:23:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Snips](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/snips/32/10214_2.png) [@Snips](https://forums.swift.org/u/Snips)\
**Post date:** [January 16, 2020, 12:23am UTC](https://forums.swift.org/t/string-to-bool/32633/1 "2020-01-16T00:23:51Z")

</div>

Hi,

I found out about SE because of a very simple coding issue, that seemed odd.

I found myself needing to convert a string representing a boolean to a Bool. It seemed straight-forward enough, and yet myBoolString.boolValue and Bool(myBoolString) were not available, and by searching on [SO](https://stackoverflow.com/questions/28107051/convert-string-to-bool-in-swift-via-api-or-most-swift-like-approach), I found that I needed to convert via NSString,

```swift
let myBool = (self as NSString).boolValue

```

This seemed rather odd to me, and I'm wondering why such a basic conversion isn't possible in Swift directly.

Any insights appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [January 16, 2020, 12:31am UTC](https://forums.swift.org/t/string-to-bool/32633/2 "2020-01-16T00:31:35Z")

</div>

`Bool` has an initializer that takes a `String`: [Apple Developer Documentation](https://developer.apple.com/documentation/swift/bool/2428231-init)

---

<div class="post-metadata">

**Author:** ![Snips](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/snips/32/10214_2.png) [@Snips](https://forums.swift.org/u/Snips)\
**Post date:** [January 16, 2020, 12:42am UTC](https://forums.swift.org/t/string-to-bool/32633/3 "2020-01-16T00:42:51Z")

</div>

Thank you @suyashsrijan, that makes more sense than going via NSString.

---

<div class="post-metadata">

**Author:** ![tito](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tito/32/11313_2.png) [@tito](https://forums.swift.org/u/tito)\
**Post date:** [April 18, 2020, 7:24pm UTC](https://forums.swift.org/t/string-to-bool/32633/4 "2020-04-18T19:24:57Z")

</div>

Except that `Bool` only takes `true` or `false`. If the string contains `0` or `1`, it returns nil. This is why I tend to use:

```
let myBool = (self as NSString).boolValue

```

It will allow you to convert these:

```
let myBool1 = ("0" as NSString).boolValue
let myBool2 = ("NO" as NSString).boolValue
let myBool3 = ("No" as NSString).boolValue
let myBool4 = ("False" as NSString).boolValue
let myBool5 = ("FALSE" as NSString).boolValue
let myBool6 = ("01" as NSString).boolValue
let myBool7 = ("YES" as NSString).boolValue
let myBool8 = ("Yes" as NSString).boolValue
let myBool9 = ("True" as NSString).boolValue
let myBool10 = ("TRUE" as NSString).boolValue

```

Something to keep in mind.

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [April 18, 2020, 10:26pm UTC](https://forums.swift.org/t/string-to-bool/32633/5 "2020-04-18T22:26:01Z")

</div>

Are these any good?

```swift
// 1
extension Bool {
    init(from: String) {
        self = (from as NSString).boolValue
    }
}

let booboo = Bool(from: "01")

// 2
extension Bool: ExpressibleByStringLiteral {
    public init(stringLiteral value: String) {
        self = (value as NSString).boolValue
    }
}

let ural: Bool = "01"

// 3
extension String {
    var boolValue: Bool { (self as NSString).boolValue }
}

let blah = "NO".boolValue

```

#2 + #3?

---

<div class="post-metadata">

**Author:** ![laborant5](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/laborant5/32/41060_2.png) [@laborant5](https://forums.swift.org/u/laborant5)\
**Post date:** [December 3, 2025, 3:28am UTC](https://forums.swift.org/t/string-to-bool/32633/6 "2025-12-03T03:28:54Z")

</div>

```swift
extension String{

    static let bools = [

        "true" :true,

        "True" :true,

        "TRUE" :true,

        "yes" :true,

        "YES" :true,

        "1" :true,

        "false" :false,

        "False" :false,

        "FALSE" :false,

        "no" :false,

        "NO" :false,

        "0" :false

    ]

    public func toBool()->Bool?{

        return String.bools[self]

    }
}

```

---

<div class="post-metadata">

**Author:** ![NotTheNHK](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/notthenhk/32/43199_2.png) [@NotTheNHK](https://forums.swift.org/u/NotTheNHK)\
**Post date:** [December 3, 2025, 3:13pm UTC](https://forums.swift.org/t/string-to-bool/32633/7 "2025-12-03T15:13:05Z")

</div>

In this case, we could streamline the whole thing to this:

```swift
extension Bool {
    init?(description: String) {
        switch description.lowercased() {
        case "true", "yes", "1": self = true
        case "false", "no", "0": self = false
        default: return nil
        }
    }
}

```
