无法从 UITextField 获取文本 - Swift - iOS [英] Cannot get text from UITextField - Swift - iOS
问题描述
我有一个用 Swift 编写的 iOS 应用.我正在努力从 UITextField 获取文本,因为我的应用程序在这行代码上崩溃了:
I have an iOS app written in Swift. I am struggling to get the text from the UITextField because my app crashes on this line of code:
let dataUser = self.userField.text
我收到以下崩溃报告:
fatal error: unexpectedly found nil while unwrapping an Optional value
我做错了什么?...我想做的就是从文本字段中获取文本.
What am I doing wrong?... all I want to do is get the text from the text field.
谢谢,丹.
推荐答案
在 Interface Builder 中检查您的 UITextField 是否正确链接到您的userField"IBOutlet:您的 UITextField,self.userField
,可能是未连接到 IBOutlet,因此该对象为零并且在尝试访问其 .text
属性时崩溃.
Check in Interface Builder that your UITextField is properly linked to your "userField" IBOutlet: your UITextField, self.userField
, is probably not hooked to the IBOutlet, so the object is nil and it crashes when trying to access its .text
property.
这篇关于无法从 UITextField 获取文本 - Swift - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!