访问超出范围Swift的变量 [英] Accessing a variable that is out of scope Swift

查看:98
本文介绍了访问超出范围Swift的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是基本的东西,但我似乎无法得到这个。我有一个函数,它接受日期选择器值,将它转换为一个字符串,将它赋值给一个变量,然后它们更新一个标签文本。



我希望能够在函数外部访问该变量,以便在prepareForSegue中使用它。到目前为止,我已经尝试了一个全局变量并在函数被调用时更新它,但似乎并不奏效,并且我试图在函数中返回值,但是我必须做到这一点,因为它也没有工作。
$ b

函数:
$ b $ pre $ func datePickerChanged(datePicker:UIDatePicker) {
var dateFormatter = NSDateFormatter()

dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle
dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle

var strDate = dateFormatter。 stringFromDate(datePicker.date)
dateTimeLabel.text = strDate
}

I希望在函数中获得 strDate 。任何帮助都非常感谢!

解决方案

您可以用不同的方法来做到这一点。您可以在方法上使用inout参数允许方法更新参数,如下所示:

  var aString =

func doStuffWithA (inout theString:String){

theString =Groovy
}

doStuffWithA(& aString)//将aString改为Groovy

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

  class SomeClass {
var someString:String =

func doStuff(){
self.someString =Groovy






$ b如果你只想要一个segue,执行SegueWithIdentifier上的对象,如下所示:

  func doStuff(){

var aString =Groovy
performSegueWithIdentifier(someSegue,发件人:aString)

}

// Th在这里你可以使用它并将它作为一个属性分配给下一个视图控制器

覆盖func prepareForSegue(segue:UIStoryboardSegue,sender:AnyObject?){

if segue。 identifier ==someSegue{

guard让aString = sender as? String else {

return
}

让nextVC = segue.destinationViewController as! SomeVC
nextVC.someProperty = aString
}
}


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.

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.

The Function:

func datePickerChanged(datePicker:UIDatePicker) {
    var dateFormatter = NSDateFormatter()

    dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle
    dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle

    var strDate = dateFormatter.stringFromDate(datePicker.date)
    dateTimeLabel.text = strDate
}

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

解决方案

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"
    }
 }

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的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆