简体   繁体   English

在Swift中,如何在函数外部的函数中使用变量声明?

[英]How do I use variable declarations within a function outside of the function in Swift?

I am new to programming and could not find out how I might get the variable data back out of the function I created in swift. 我是编程新手,无法找到如何从迅速创建的函数中取回变量数据。 I tried returning the variable however I get an error in the compiler. 我尝试返回变量,但是在编译器中出现错误。

    class ViewController: UIViewController {


    @IBOutlet var textField: UITextField!

    var userName = String()

    @IBAction func returnPressed(_ sender: Any) {

        userName = textField.text! //user input will = userName
        print("return pressed inside textfield")
        print("User name set to \(userName)")

    self.view.endEditing(true) //hides keyboard
        return (userName)
    }

The function activates via the return being button pressed. 通过按下返回按钮可以激活该功能。 The variable userName will then equal the users input within the text field. 然后,变量userName将等于在文本字段中输入的用户。

The problem is that outside of this function, I can no longer call on the variable userName and receive the user input value that was stored earlier. 问题在于,在此函数之外,我无法再调用变量userName并接收之前存储的用户输入值。 And I cant seem to get the "return" to work like ive been reading. 而且我似乎无法像我一直在读书一样获得“回报”。

The function's return statement isn't doing anything because you are not declaring the function as returning anything. 该函数的return语句没有执行任何操作,因为您没有将函数声明为返回任何东西。

func returnPressed(_ sender : Any) -> String

That would be a function that returns a String. 那将是一个返回字符串的函数。

In this case, you don't need a return statement because you are not capturing the function's result anywhere by calling the function manually. 在这种情况下,您不需要return语句,因为您不会在任何地方通过手动调用函数来捕获函数的结果。

If you print the value of userName elsewhere in your code after pressing Return, the variable will have captured that value, please make sure to try before saying you're sure it's not working :) 如果在按Return键后在代码的其他位置打印userName的值,则该变量将捕获该值,请确保在尝试确定它不起作用之前先尝试一下:)

First of all userName is an internal variable for your class ViewController and can be accessed in every function. 首先, userName是类ViewController的内部变量,可以在每个函数中进行访问。 You have to access the variable within some function like viewDidLoad , viewWillAppear or any of your custom function within your class. 您必须在某些函数(例如viewDidLoadviewWillAppear或类中的任何自定义函数中访问变量。

Make any function and access the variable 进行任何功能并访问变量

func test() {
    //access here userName
}

All entities in your code (with a few specific exceptions) have a default access level of internal if you do not specify an explicit access level yourself. 如果您自己未指定显式访问级别,则代码中的所有实体(有一些特定的例外)都具有内部默认访问级别。 As a result, in many cases you do not need to specify an explicit access level in your code. 因此,在许多情况下,您无需在代码中指定显式访问级别。

Read more about Access Controls . 阅读有关访问控制的更多信息。

Also you are returning something from an IBAction of the button which don't have any return type. 另外,您还从按钮的IBAction返回没有任何返回类型的内容。 You will get a compiler error there. 您将在此处收到编译器错误。

Generally return of textfields is handled by UITextFieldDelegate . 通常,返回文本字段由UITextFieldDelegate处理。 See here . 这里

The delegate method in Swift 3 is Swift 3中的委托方法是

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    return true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM