XCode8 / Swift不会让我设置textField的文本等于一个字符串?(XCode8 / Swift will not let me set a textField's text

编程入门 行业动态 更新时间:2024-10-28 11:29:30
XCode8 / Swift不会让我设置textField的文本等于一个字符串?(XCode8 / Swift will not let me set a textField's text equal to a String?)

我对iOS应用程序编程非常陌生(刚刚在一周前开始学习!),所以我确定我要问的是非常明显的!

所以我正在为我的工作学习职位申请一个应用程序,并且不能为我的生活弄清楚我做错了什么。 基本上,我想要做的就是在允许用户继续前确认每个字段是否已填满。 我在另一个视图控制器中做了类似的事情,以检查输入的内容是否与我为该程序设置的密码相同(我相信有更好的方法可以做到这一点,但是因为我是自学的,而且是我的第一次申请,我并不担心,我有一整年的时间来擦亮它!)

这里是代码(如果你有建议可以改善它,请随时提供,但大多数情况下,我只是想弄明白为什么这会起作用,而其他代码却不行)

@IBAction func enterPassword(_ sender: Any) { pw = password.text! let message = "Incorrect Password" if(pw == "RISE") { performSegue(withIdentifier: "ToMenu", sender: self) } else { incorrect.text = message } }

请注意变量“pw”是一个全局变量,并在类之外声明。 密码和不正确都是先前声明的标签。

这是有问题的代码。 我不明白为什么我得到这个错误。 它是“线程1:exc_bad_instruction”。 我真的不知道这个错误是什么意思,所以如果你碰巧知道它是什么意思,请赐教! 我将这个问题包含在viewController的所有代码中。

import UIKit var studName = " " class CreateViewController: UIViewController { @IBOutlet weak var name: UITextField! @IBOutlet weak var ID: UITextField! @IBOutlet weak var YR: UITextField! @IBOutlet weak var test: UILabel! @IBAction func endCreate(_ sender: Any) { studName = name.text! } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ }

我在这里做错了什么? 我非常不确定和困惑。 我非常擅长使用Java和C ++进行编码,所以我从来没有想过会有学习上的问题,但它完全不同,所花的时间比我预期的要长很多(花了我大约一周的时间学习C#...)。

谢谢你的帮助!

编辑:(我需要10个声望发布两个以上的链接,所以我删除了错误。大家好,我想我会去回答一些问题或某事)

点击这里查看StoryBoard

点击这里查看与NAME的连接(注意我重新连接并使用了全部大写,错误仍然是一个问题)

如果有任何其他信息可以帮助您理解,请告诉我! 谢谢!

I am very new to iOS application programming (started learning just under a week ago!) so I'm sure what I'm asking is very obvious!

So I am working on an Application for my work study position and cannot for the life of me figure out what I'm doing wrong. Basically, all I want to do is check to make sure each field is filled in before allowing the user to carry on. I did a similar thing in another view controller to check to see if what was entered is equal to the password I set for the program (I am sure there is a better way to do this, but because I'm self learning and it's my first application, I'm not worried too much. I have a whole year to polish it!)

Here is the code for that (if you have suggestions that may improve it, feel free, but for the most part, I'm just trying to figure out why this worked and the other code is not)

@IBAction func enterPassword(_ sender: Any) { pw = password.text! let message = "Incorrect Password" if(pw == "RISE") { performSegue(withIdentifier: "ToMenu", sender: self) } else { incorrect.text = message } }

please note that the variable "pw" is a global variable and is declared outside of the class. Password and incorrect are both labels previously declared as well.

Here is the code that is in question. I do not understand why I am getting an error for this. it is "thread 1: exc_bad_instruction". I'm honestly not even sure what this error means so if you happen to know what it means, please enlighten me! I included all the code for the viewController with the issue.

import UIKit var studName = " " class CreateViewController: UIViewController { @IBOutlet weak var name: UITextField! @IBOutlet weak var ID: UITextField! @IBOutlet weak var YR: UITextField! @IBOutlet weak var test: UILabel! @IBAction func endCreate(_ sender: Any) { studName = name.text! } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ }

What am I doing wrong here? I am really uncertain and confused. I am pretty good at coding in Java and C++ so I never expected to have issues learning but it's totally different and is taking a lot longer than I expected (It took me about a week to learn C#...).

Thanks for any help!

EDIT: (I need 10 reputation to post more than two links, so I deleted the error. lol I guess I'll go answer some questions or something)

Click here to see StoryBoard

Click here to see Connection to NAME (note I reconnected it and used all caps, error is still an issue)

if there is any other information that may help you understand, please let me know! Thanks!

最满意答案

我在这里假设你正在经历这一行的崩溃

studName = name.text!

? (如果不能,请包括堆栈跟踪以指示您遇到问题的位置?)

它看起来像是name是nil ,或者text是nil ,这两个都在这里被强制解包。 所以要么你没有在界面生成器中正确连接这个文本域,要么没有文本。

我的一般规则是(我相信人们会不同意这一点):

永远不要使用!

这样一个简单的规则:)

我的理由是,解包一个可选变量的强制永远不会(非常非常少)需要,并且暗示了一个明确意图允许为零的值的假设(否则它将不是可选的)。 通常,如果您发现自己使用! 在你自己的一个变量上,考虑重构变量不是可选的,否则大量使用可选的绑定和/或保护语句来避免强制事件。

(我对此的一个主要警告是创建实例变量,它需要self作为初始化的参数,其中实例变量是隐式解包的可选项,但是在调用super的初始化程序后立即设置,并且不会再次)

另外,虽然它起作用并且是有效的代码,但是对于诸如studName东西使用全局变量并不studName ,这应该是控制器中的实例变量。

如果您对此有任何其他问题(或iOS / Swift),请随时留言。 如果您刚开始使用,我无法推荐http://web.stanford.edu/class/cs193p ! 全面了解所有核心iOS技术和设计模式。

编辑评论

在这种情况下,你可以避免使用这个! 通过使你的变量可选,你可以使用声明studName

var studName: String? = " "

这使得它成为一个可选变量,它将允许您为它指定一个零值。

如果你确定你不希望它是零,你也可以做你的任务

studName = name.text ?? " "

这将检查name.text的值,如果它不是nil分配给studName ,否则它将分配右边的值" " 。 它基本上允许您为不可选的分配提供默认值。 这是速记:

if let text = name.text { studName = text } else { studName = " " }

I'm assuming here that you are experiencing the crash on this line

studName = name.text!

? (If not could you please include the stack trace to indicate where you are getting an issue?)

It looks like either name is nil, or text is nil both of which are being force unwrapped here. So either you haven't connected this text field properly in interface builder, or there is just no text in it.

My general rule is (and I'm sure people will disagree with this):

NEVER use !

Such a simple rule :)

I reason this that, force unwrapping an optional variable is never (very, very rarely) required, and implies an assumption of a value that was clearly intended to be allowed to be nil (otherwise it would not be optional). Typically, if you find yourself using a ! on one of your own variables, consider refactoring the variable to not be optional, otherwise make heavy use of optional binding and/or guard statements to avoid forcing things.

(My one main caveat for this is creating instance variables that require self as a parameter for initialisation. Where the instance variable is an implicitly unwrapped optional, but is set immediately after the call to super's initialiser, and never again)

Also, while it works and is valid code, it is not typical to use global variables for things like studName, this should be an instance variable in your controller.

Do feel free to shout if you have any other questions about this (or anything iOS/swift). If you're just getting started, I can't recommend http://web.stanford.edu/class/cs193p highly enough! A thorough rundown of all core iOS technologies and design patterns.

EDIT for comment

In this case you could avoid using this ! by making your variable optional, you could declare studName using

var studName: String? = " "

this makes it an optional variable which will allow you to assign a nil value to it.

If you are sure you don't want it to be nil, you could also do your assignment as

studName = name.text ?? " "

This will check the value of name.text and if it is not nil assign to studName, otherwise it will assign the value on the right " ". It essentially allows you to provide an default value for an assignment that is not optional. It is shorthand for:

if let text = name.text { studName = text } else { studName = " " }

更多推荐

本文发布于:2023-04-27 11:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327101.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:让我   字符串   文本   textField   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!