Convert String to Bool in Swift - via API or Most Swift-Like Approach

Convert String to Bool in Swift - via API or most Swift-like approach

Typecasting along with a nice String extension and you're up and running

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

Swift 3: Converting numeric string to Bool, getting false for invalid values

For that you can use Nil-Coalescing Operator.

let boolValue = (Int(stringValue) ?? 0) != 0

How to convert Swift Bool? - String?

let b1: Bool? = true
let b2: Bool? = false
let b3: Bool? = nil

print(b1?.description ?? "none") // "true"
print(b2?.description ?? "none") // "false"
print(b3?.description ?? "none") // "none"

or you can define 'one liner' which works with both Bool and Bool? as a function

func BoolToString(b: Bool?)->String { return b?.description ?? "<None>"}

Swift native code returns iOS version instead of bool

Turns out I was calling it incorrectly and it was returning the version number due to the default Flutter AppDelegate template -

public class SwiftPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "flutter", binaryMessenger: registrar.messenger())
let instance = SwiftPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
result("iOS " + UIDevice.current.systemVersion)
}
}

Casting to String and Bool from firebase function

Your data contains 1 and lala as values, so it cannot be casted to [String: Bool] or [String: String].
Values of Swift dictionaries should have a same type.
First cast it to [String: Any] and then cast rawTime from Any to Int and rawString from Any to String:


guard
let rawTime = (result?.data as? [String: Any])?["serverTime"],
let time = rawTime as? Int
else { return }
print(time)

guard
let rawString = (result?.data as? [String: Any])?["example"],
let string = rawString as? String
else { return }
print(string)

Using JSONSerialization() to dynamically figure out boolean values

When you use JSONSerialization, any Bool values (true or false) get converted to NSNumber instances which is why the use of is Double, is Int, and is Bool all return true since NSNumber can be converted to all of those types.

You also get an NSNumber instance for actual numbers in the JSON.

But the good news is that in reality, you actually get special internal subclasses of NSNumber. The boolean values actually give you __NSCFBoolean while actual numbers give you __NSCFNumber. Of course you don't actually want to check for those internal types.

Here is a fuller example showing the above plus a workable solution to check for an actual boolean versus a "normal" number.

let jsonString = "{\"boolean_key\" : true, \"int_key\" : 1}"
let jsonData = jsonString.data(using: .utf8)!
let json = try! JSONSerialization.jsonObject(with: jsonData, options: []) as! [String:Any]

print(type(of: json["boolean_key"]!)) // __NSCFBoolean
json["boolean_key"] is Double // true
json["boolean_key"] is Int // true
json["boolean_key"] is Bool // true

print(type(of: json["int_key"]!)) // __NSCFNumber
json["int_key"] is Double // true
json["int_key"] is Int // true
json["int_key"] is Bool // true

print(type(of: json["boolean_key"]!) == type(of: NSNumber(value: true))) // true
print(type(of: json["boolean_key"]!) == type(of: NSNumber(value: 1))) // false
print(type(of: json["int_key"]!) == type(of: NSNumber(value: 0))) // true
print(type(of: json["int_key"]!) == type(of: NSNumber(value: true))) // false


Related Topics



Leave a reply



Submit