<small id='abyYh'></small><noframes id='abyYh'>

    1. <tfoot id='abyYh'></tfoot>
        <bdo id='abyYh'></bdo><ul id='abyYh'></ul>

      <legend id='abyYh'><style id='abyYh'><dir id='abyYh'><q id='abyYh'></q></dir></style></legend>
      <i id='abyYh'><tr id='abyYh'><dt id='abyYh'><q id='abyYh'><span id='abyYh'><b id='abyYh'><form id='abyYh'><ins id='abyYh'></ins><ul id='abyYh'></ul><sub id='abyYh'></sub></form><legend id='abyYh'></legend><bdo id='abyYh'><pre id='abyYh'><center id='abyYh'></center></pre></bdo></b><th id='abyYh'></th></span></q></dt></tr></i><div id='abyYh'><tfoot id='abyYh'></tfoot><dl id='abyYh'><fieldset id='abyYh'></fieldset></dl></div>
    2. 访问超出范围 Swift 的变量

      Accessing a variable that is out of scope Swift(访问超出范围 Swift 的变量)

        <small id='jlY7n'></small><noframes id='jlY7n'>

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

                <bdo id='jlY7n'></bdo><ul id='jlY7n'></ul>
                <tfoot id='jlY7n'></tfoot>
                <legend id='jlY7n'><style id='jlY7n'><dir id='jlY7n'><q id='jlY7n'></q></dir></style></legend>
                本文介绍了访问超出范围 Swift 的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我知道这是基本的东西,但我似乎无法理解.我有一个函数,它获取日期选择器值,将其转换为字符串,将其分配给变量,然后更新标签文本.

                I know the is elementary stuff but I can't seem to get this. I have a function that takes the date pickers value, converts it to a string, assigns it to a variable, and them updates a labels text.

                我希望能够在函数之外访问该变量,以便在 prepareForSegue 中使用它.到目前为止,我已经尝试创建一个全局变量并在调用函数时对其进行更新,但这似乎不起作用,并且我尝试在函数中返回值但我一定做错了,因为它也不起作用.

                I want to be able to access that variable outside of the function so I can use it in prepareForSegue. So far I have tried making a global variable and updating it when the function is called but that didn't seem to work, and I have tried returning the value in the function but I must have done that wrong because it didn't work either.

                功能:

                func datePickerChanged(datePicker:UIDatePicker) {
                    var dateFormatter = NSDateFormatter()
                
                    dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle
                    dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
                
                    var strDate = dateFormatter.stringFromDate(datePicker.date)
                    dateTimeLabel.text = strDate
                }
                

                我想从函数中取出 strDate.非常感谢任何帮助!

                I want to get strDate out of the function. Any help is much appreciated!

                推荐答案

                你可以用不同的方式做到这一点.你可以在方法上使用 inout 参数来允许方法更新参数,像这样:

                You can do this in different ways.. You can use an inout parameter on a method to allow the method to update the parameter, like this:

                var aString = ""
                
                func doStuffWithA(inout theString: String) {
                
                theString = "Groovy"
                }
                
                doStuffWithA(&aString) // changes aString to "Groovy"
                

                或者你可以在方法之外声明属性:

                Or you can declare the property outside of the method:

                class SomeClass {
                    var someString: String = ""
                
                    func doStuff() {
                        self.someString = "Groovy"
                    }
                 }
                

                如果你只想要一个 segue,你可以在 performSegueWithIdentifier 上传递对象,像这样:

                If you want this just for a segue, you can pass the object on performSegueWithIdentifier, like this:

                func doStuff() {
                
                    var aString = "Groovy"
                    performSegueWithIdentifier("someSegue", sender: aString)
                
                }
                
                // Then here you can use it and assign it as a property on the next view controller
                
                override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
                
                    if segue.identifier == "someSegue" {
                
                         guard let aString = sender as? String else {
                
                         return
                         }
                
                         let nextVC = segue.destinationViewController as! SomeVC
                         nextVC.someProperty = aString
                    }
                }
                

                这篇关于访问超出范围 Swift 的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 之间有什么区别吗?)
                Set bool property of all objects in the array(设置数组中所有对象的布尔属性)

                <small id='kMNKe'></small><noframes id='kMNKe'>

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

                    • <legend id='kMNKe'><style id='kMNKe'><dir id='kMNKe'><q id='kMNKe'></q></dir></style></legend>
                      • <bdo id='kMNKe'></bdo><ul id='kMNKe'></ul>