String to Bool

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, I found that I needed to convert via NSString,

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.

Bool has an initializer that takes a String: https://developer.apple.com/documentation/swift/bool/2428231-init

2 Likes

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

Terms of Service

Privacy Policy

Cookie Policy