将文本字段中的文本保存为变量 [英] save the text in a text field as a variable

查看:27
本文介绍了将文本字段中的文本保存为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个小时但没有成功,所以希望你们中的一个人会感到遗憾.我使用的是 Xcode 9 和 Swift 4.

I have been searching for an hour with no success so hope one of you will have pity. I am using Xcode 9 and Swift 4.

textIt.text = name

完美运行,我已经在我的应用程序中使用了它

Works perfectly and I have used it in my app but

name = textIt.text

显示错误代码:

可选类型字符串?"的值未解开;你的意思是使用!"还是?"?

Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?

我想要做的就是将变量中的一些文本放入字段中,以便用户可以更改它,然后我将文本放回变量中.

All I want to do is to put some text from a variable into a field so the user can change it then I put the text back into the variable.

我做错了什么?

推荐答案

您可以在设置和获取 textIt 时使用称为 Computer Property 的东西跟踪name"变量.这将允许您将变量名称和文本字段联系起来.

You can track "name" variable when setting and getting textIt using something called Computer Property. This will allow you to tie your variable name and your text field.

var name:String? {

    get {
        return textIt.text
    }

    set {
        textIt.text = newValue
    }
}

当您设置 name 变量时,textIt 将自动设置.另一方面,当您获取 name 变量时,我将从 textIt 获取值.

When you set your name variable, textIt will automatically will be set. On the otherhand, when you get the name variable, i will get the value from textIt.

由于 name 变量是可选的,您可以使用以下代码解包.

Since the name variable is optional you can use the following code to unwrap.

如果让 userName = name {

If let userName = name {

}

这篇关于将文本字段中的文本保存为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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