1
我得到這個錯誤,我無法解釋:發現零而展開的可選值 - 斯威夫特2
@IBOutlet weak var licencePlateLabel: UILabel!
var editItem: CarValues? {
didSet {
// Update the view.
self.configureView()
}
}
func configureView() {
// Update the user interface for the detail item.
if let editedCar = self.editItem {
if let licencePlate = self.licencePlateLabel {
licencePlate.text? = editedCar.licencePlate!//this gives me nil
}
else {
print("value was nil")
}
print(editedCar.licencePlate!)//this is giving me the correct value
}
如果我更換
if let licencePlate = self.licencePlateLabel {
licencePlate.text! = editedCar.licencePlate!
}//this throws an error "found nil......"
即使我做到這一點我還在原地得到在「發現零...」
func configureView() {
licencePlateLabel.text = "test"
[...]
}
,但如果我把上面的viewDidLoad中然後正常工作
override func viewDidLoad() {
licencePlateLabel.text = "test"
[...]
}
這段代碼是怎麼回事?
編輯
我傳遞的editItem的值從的DetailView到EditView中是這樣的:
@IBAction func editButtonTapped(sender: AnyObject) {
let storyBoard = UIStoryboard(name: "Main", bundle:nil)
let editScreen = storyBoard.instantiateViewControllerWithIdentifier("ID_EditViewController")
self.navigationController?.pushViewController(editScreen, animated: true)
let controller = EditViewController()
controller.editItem = detailItem
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
我仍然得到零,如果我這樣做 –
@GeorgeAsda你確定'editedCar'這裏有一個licencePlate值嗎?你可以嘗試改變你的if-let是:'如果讓editedCar = self.editItem,其中editedCar.licencePlate!= nil',並設置一個斷點,看看是否符合條件。 –
不應該在打印(editedCar.licencePlate!)之前調用if if let給nil以及嗎? –