简体   繁体   English

Swift中的致命错误,使用来自Parse的数据展开可选值

[英]Fatal Error in Swift unwrapping an Optional value using data from Parse

I am trying to display a currently logged in username and I am getting a fatal error about an unexpected nil for an optional value. 我正在尝试显示当前登录的用户名,但出现致命错误,提示有关可选值的意外nil。 I've searched several of the other questions related to my issue (using "if let") and attempted the solution but for some reason I cannot get this code to display in my label even though I am ,in fact, showing a value for my variable on debug. 我已经搜索了与我的问题相关的其他几个问题(使用“ if let”)并尝试了解决方案,但是由于某种原因,即使我实际上显示了一个值,我也无法在我的标签中显示此代码。我的调试变量。 My code is as follows: 我的代码如下:

import UIKit
import Parse

class DashboardViewController: UIViewController {

@IBOutlet var welcomeText: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()


}

override func viewWillAppear(animated: Bool) {

    if  let currentUser = PFUser.currentUser()?.username {

        welcomeText.text = "Welcome  \(currentUser)!"

    } else {

        welcomeText.text = "Welcome"
    }


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

  }
}

I have even tried (as I posted this) moving my code to the viewWillAppear() function to see if that made a difference considering that has helped in the past, but no luck. 我什至尝试过(如我所发表的那样)将代码移至viewWillAppear()函数,以考虑到过去曾经有所帮助,但是没有运气,因此看是否有所作为。 Any help would be greatly appreciated! 任何帮助将不胜感激!

error: 错误: 如您在这里看到的,我在调试时有一个值,右下角是错误。

It's your label that is nil. 是您的标签为零。 It looks to me like the connection for the label between Interface Builder and your code got disconnected. 在我看来,Interface Builder和您的代码之间的标签连接已断开。 You'll need to right click and drag from the label to the outlet property to form the connection again. 您需要右键单击并从标签拖动到插座属性以再次形成连接。

Try below code, put the parse code separate. 尝试下面的代码,将解析代码分开。 I guess you try to assign the Parse data before it is ready. 我猜您尝试在准备好之前分配解析数据。

import UIKit
import Parse

class DashboardViewController: UIViewController {

@IBOutlet var welcomeText: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()
welcomeMessage()

}

override func viewWillAppear(animated: Bool) {

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

  }

    func welcomeMessage () {
    if  let currentUser = PFUser.currentUser()?.username {

            welcomeText.text = "Welcome  \(currentUser)!"

        } else {

            welcomeText.text = "Welcome"
        }
    }

}

It still would be very helpful to see more code, where is your code to fetch the user? 看到更多代码仍然非常有帮助,您的代码在哪里获取用户? Did you try to set the label Text there as well? 您是否也尝试过在此处设置标签“文本”? Did that work? 那行吗?

暂无
暂无

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

相关问题 致命错误:解开可选值时意外发现nil(Swift&Parse) - fatal error: unexpectedly found nil while unwrapping an Optional value (Swift & Parse) Swift:致命错误:在展开可选值时意外发现nil - Swift : fatal error: unexpectedly found nil while unwrapping an Optional value 快速致命错误:解开Optional值时意外发现nil - swift fatal error: unexpectedly found nil while unwrapping an Optional value Swift致命错误:解开Optional值时意外发现nil - Swift fatal error: unexpectedly found nil while unwrapping an Optional value 致命错误:在Swift 3中解开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value in Swift 3 SWIFT-致命错误:解开可选值时意外发现nil - SWIFT - fatal error: unexpectedly found nil while unwrapping an Optional value 致命错误:解开可选值时意外发现nil-核心数据(Swift) - Fatal error: unexpectedly found nil while unwrapping an Optional value - Core Data (Swift) 致命错误:在使用Swift时解开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value while using Swift 线程 1:致命错误:在展开可选值时意外发现 nil。 从图库 swift 获取图像 - Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value. Getting image from Gallery swift iOS Swift:致命错误,在UICollectionView中展开可选 - iOS Swift: Fatal error with unwrapping optional in UICollectionView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM