<bdo id='SI0BD'></bdo><ul id='SI0BD'></ul>

    1. <small id='SI0BD'></small><noframes id='SI0BD'>

        <tfoot id='SI0BD'></tfoot><legend id='SI0BD'><style id='SI0BD'><dir id='SI0BD'><q id='SI0BD'></q></dir></style></legend>

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

      1. Swift:UIButton textLabel.text 值在 switch 语句中不可用

        时间:2023-07-06
            • <small id='5WXRK'></small><noframes id='5WXRK'>

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

              <tfoot id='5WXRK'></tfoot>
                  <bdo id='5WXRK'></bdo><ul id='5WXRK'></ul>
                    <tbody id='5WXRK'></tbody>
                • 本文介绍了Swift:UIButton textLabel.text 值在 switch 语句中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 Swift 和 iOS 开发的新手,所以我正在尝试构建一个用于学习目的的计算器应用程序.但是,我遇到了一个错误.我已经用它们代表的数字为我的所有按钮命名,所以我通过 sender.titleLabel.text 在 buttonPress IBAction 中检索标题.然后,我将其传递给 switch 语句,以确定按钮是数字还是运算符.

                  I am new to Swift and iOS development, so I am trying to build a calculator app for learning purposes. However, I am encountering an error. I have titled all of my buttons with the number they represent, so I am retrieving the title in the buttonPress IBAction via sender.titleLabel.text. Then, I pass that into a switch statement to determine if the button was a number or an operator.

                  func handleButton (sender:UIButton) {
                  switch sender.titleLabel.text {
                  case "1","2","3","4","5","6","7","8","9","0" :
                      println(sender.titleLabel.text)
                  default:
                      break
                      }     
                  }
                  

                  错误是 sender.titleLabel.text 不会绑定到我输入的字符串值 - 也不会绑定到任何字符串值 - 即使它是 String 类型.

                  The error is that sender.titleLabel.text will not bind to the string values I have entered - nor any string values - even though it is is of type String.

                  推荐答案

                  目前编译器中似乎存在一个错误,即不能在 switch 语句中使用 Implicitly Unwrapped Optionals.相反,您可以使用可选绑定来满足编译器的要求.作为一个额外的好处,这将处理 titleLabel.text 为 nil 的情况.

                  There seems to be a bug in the complier at the moment where Implicitly Unwrapped Optionals cannot be used in switch statements. Instead you can use optional binding to satisfy the compiler. As an extra plus, this will handle the case where titleLabel.text is nil.

                  func handleButton (sender:UIButton) {
                      if let text = sender.titleLabel.text {
                          switch text {
                              case "1","2","3","4","5","6","7","8","9","0" :
                                  println(sender.titleLabel.text)
                              default:
                                  break
                          }
                      }
                      else {
                          // sender.titleLabel.text is nil
                      }
                  }
                  

                  这篇关于Swift:UIButton textLabel.text 值在 switch 语句中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Swift 中将 UIButton 覆盖在 UIWebView 上 下一篇:我的按钮在 iPhone 6 和 6 plus 上居中,但在 iPhone 5 上不居中

                  相关文章

                    <bdo id='DotDD'></bdo><ul id='DotDD'></ul>

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

                    1. <small id='DotDD'></small><noframes id='DotDD'>

                      <legend id='DotDD'><style id='DotDD'><dir id='DotDD'><q id='DotDD'></q></dir></style></legend>