• <tfoot id='23Dju'></tfoot>
      <bdo id='23Dju'></bdo><ul id='23Dju'></ul>
    <legend id='23Dju'><style id='23Dju'><dir id='23Dju'><q id='23Dju'></q></dir></style></legend>

        <small id='23Dju'></small><noframes id='23Dju'>

      1. <i id='23Dju'><tr id='23Dju'><dt id='23Dju'><q id='23Dju'><span id='23Dju'><b id='23Dju'><form id='23Dju'><ins id='23Dju'></ins><ul id='23Dju'></ul><sub id='23Dju'></sub></form><legend id='23Dju'></legend><bdo id='23Dju'><pre id='23Dju'><center id='23Dju'></center></pre></bdo></b><th id='23Dju'></th></span></q></dt></tr></i><div id='23Dju'><tfoot id='23Dju'></tfoot><dl id='23Dju'><fieldset id='23Dju'></fieldset></dl></div>

      2. 在 Swift 中将 String 转换为 Bool - 通过 API 或大多数类似 Swift 的方法

        Convert String to Bool in Swift - via API or most Swift-like approach(在 Swift 中将 String 转换为 Bool - 通过 API 或大多数类似 Swift 的方法)
      3. <small id='P6VO9'></small><noframes id='P6VO9'>

        <i id='P6VO9'><tr id='P6VO9'><dt id='P6VO9'><q id='P6VO9'><span id='P6VO9'><b id='P6VO9'><form id='P6VO9'><ins id='P6VO9'></ins><ul id='P6VO9'></ul><sub id='P6VO9'></sub></form><legend id='P6VO9'></legend><bdo id='P6VO9'><pre id='P6VO9'><center id='P6VO9'></center></pre></bdo></b><th id='P6VO9'></th></span></q></dt></tr></i><div id='P6VO9'><tfoot id='P6VO9'></tfoot><dl id='P6VO9'><fieldset id='P6VO9'></fieldset></dl></div>

            • <bdo id='P6VO9'></bdo><ul id='P6VO9'></ul>
                <tbody id='P6VO9'></tbody>
                <tfoot id='P6VO9'></tfoot>
                <legend id='P6VO9'><style id='P6VO9'><dir id='P6VO9'><q id='P6VO9'></q></dir></style></legend>
                  本文介绍了在 Swift 中将 String 转换为 Bool - 通过 API 或大多数类似 Swift 的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否有一个 API 可以转换布尔值的大多数可能的字符串表示形式(例如True"、true"、False"、false"、yes"、no"、1"、0")在 Swift 中变成 Bool?

                  Is there an API to convert most possible String representations of Boolean values (e.g. "True", "true", "False", "false", "yes", "no", "1", "0") into a Bool in Swift?

                  如果不是,那么从头开始编码最类似于 Swift 的方法是什么?它会是一个功能性的 map() 操作吗?还是别的什么?

                  If not, what would be the most Swift-like approach to coding this from scratch? Would it be a functional map() operation? Or something else?

                  此实例中的原始源数据是 JSON,但我对以最类似于 Swift 的方式解决问题的关键点感兴趣,从而在此过程中更多地了解该语言.

                  The original source data in this instance is JSON, but I'm interested in the crux of solving the problem in the most Swift-like way possible and hence learning more about the language in the process.

                  推荐答案

                  AFAIK 没有内置方式.与标准 toInt() 类似的方法可能是:

                  There is not built in way AFAIK. Similar method to standard toInt() could be:

                  extension String {
                      var bool: Bool? {
                          switch self.lowercased() {
                          case "true", "t", "yes", "y":
                              return true
                          case "false", "f", "no", "n", "":
                              return false
                          default:
                              if let int = Int(string) {
                                  return int != 0
                              }
                              return nil
                          }
                      }
                  }
                  

                  这篇关于在 Swift 中将 String 转换为 Bool - 通过 API 或大多数类似 Swift 的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  SharedPreferences amp; boolean(SharedPreferences amp;布尔值)
                  What values should I use for iOS boolean states?(我应该为 iOS 布尔状态使用什么值?)
                  Converting Int to Bool(将 Int 转换为 Bool)
                  Evaluate Bool property of optional object in if statement(在 if 语句中评估可选对象的 Bool 属性)
                  How do I get NSJSONSerialization to output a boolean as true or false?(如何让 NSJSONSerialization 将布尔值输出为真或假?)
                  Is there any difference between bool, Boolean, and BOOL in Objective-C?(Objective-C 中的 bool、Boolean 和 BOOL 之间有什么区别吗?)
                  • <bdo id='1yHsy'></bdo><ul id='1yHsy'></ul>
                      <tbody id='1yHsy'></tbody>
                    <legend id='1yHsy'><style id='1yHsy'><dir id='1yHsy'><q id='1yHsy'></q></dir></style></legend>

                        <small id='1yHsy'></small><noframes id='1yHsy'>

                        <tfoot id='1yHsy'></tfoot>
                        • <i id='1yHsy'><tr id='1yHsy'><dt id='1yHsy'><q id='1yHsy'><span id='1yHsy'><b id='1yHsy'><form id='1yHsy'><ins id='1yHsy'></ins><ul id='1yHsy'></ul><sub id='1yHsy'></sub></form><legend id='1yHsy'></legend><bdo id='1yHsy'><pre id='1yHsy'><center id='1yHsy'></center></pre></bdo></b><th id='1yHsy'></th></span></q></dt></tr></i><div id='1yHsy'><tfoot id='1yHsy'></tfoot><dl id='1yHsy'><fieldset id='1yHsy'></fieldset></dl></div>