在 UILabel 上解包可选值时意外发现 nil



在我的项目中,我实现了一个协议,它进行一些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 中,它具有文本出口VIew

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() 中设置了文本,它不会崩溃。 但是当我尝试在委托函数中设置值时,它崩溃说发现 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")
           }
    }

*

最新更新