繁体   English   中英

在UILabel上解开Optional值时意外发现nil

[英]unexpectedly found nil while unwrapping an Optional value On UILabel

在我的项目中,我实现了一个协议,该协议可以进行一些URL调用并返回结果,而我的意图是在UILabel中显示结果。 以下是我的代码:

protocol RestAPIResult
{
   func retriveDriverInfo()
}

class RestAPICall :  RestAPIResult
{
   func retriveDriverInfo()
    {
     self.dashBoardViewController.getDriverInfo(driverProfile)
    // calling another function of Next View for Lable Setup
    }
}

getDriverInfo在NextView中,该出口具有textVIew的出口

class DashBoardViewController: UIViewController
{
       override func viewDidLoad()
        {
        super.viewDidLoad()

        restDeledate = RestAPICall()

        restDeledate!.retriveDriverInfo()
     // IF LABEL SET HERE NO ERROR
      //totalTripLabel.text = "Testing"  // NO ERROR

        }

       func getDriverInfo(driverInfoArray : NSArray)
       {
        totalTripLabel.text = "Testing" // HERE IS ERROR
       }

}

如果在ViewDidLoad()中设置了Text,则不会崩溃。 但是,当我尝试在委托函数中设置值时,它崩溃,提示找到null。

protocol RestAPIResult: NSObjectProtocol
    {
       func retriveDriverInfo(message: String)
    }

    class RestAPICall :  RestAPIResult
    {

    override func viewDidLoad()
            {
            super.viewDidLoad()
             //create an instance of your vc and
            instance.delegate = self
            //if you code for push vc then write this line there  insatance.delegate = self
            }
       func retriveDriverInfo(message: String)
        {
         yourLbaelOutlet.text = message
        }
    }


    class DashBoardViewController: UIViewController
    {
           weak var delegate: RestAPIResult!

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

           func getDriverInfo(driverInfoArray : NSArray)
           {
            self.delegate.retriveDriverInfo(message: "youMessage")
           }

    }

*

暂无
暂无

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

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