2 일 동안 Xcode의 Swift에 문제가 있습니다. 그 답을 모른다. 이 문제를 조사했지만 나에게 맞는 솔루션을 찾지 못했습니다. 이틀 전에 다른 문제로 인해 변경했습니다.이 link을 참조하십시오.치명적인 오류가있는 Xcode 6.1에서 예외를 해결하는 방법 : 선택 값을 언 래핑하는 동안 예상치 않게 nil을 발견했습니다.
이제 다른보기 컨트롤러의 요소를 처리 할 때 예외 메시지 unexpectedly found nil while unwrapping an Optional value
이 발생한다는 문제점이 있습니다. 내 작은 예제에서는 두 개의보기 컨트롤러 (ViewController, ViewController2) 있습니다. ViewController에서 단추를 추가하고 ViewController2에 레이블을 추가했습니다. 두 요소 모두 올바르게 참조됩니다. ViewController의 단추를 클릭하면 ViewController2의 레이블 텍스트를 인쇄하려고합니다. 그런 다음 fatal error: unexpectedly found nil while unwrapping an Optional value
이 표시됩니다.
아마 Xcode 6.1에 일반적인 문제가 있습니까? 문제를 어떻게 해결할 수 있습니까? 여기
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func onClick(sender: AnyObject) {
let view2 = self.storyboard?.instantiateViewControllerWithIdentifier("view2") as ViewController2;
//IN THIS LINE I GET THE FATAL ERROR
println(view2.label.text)
}
}
이 ViewController2 내 코드입니다 : 여기
가의 ViewController에 대한 내 코드입니다
class ViewController2: UIViewController {
@IBOutlet var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
"어떻게 문제를 해결할 수 있습니까?" 1 단계 : 메시지를 믿어야합니다. 'view2'는 nil이거나'label'은 nil이거나'text'는 nil입니다. 왜 약간의 로깅/디버깅을하고 어떤 것이 있는지 알아보십시오. 2 단계 : 그렇다면 자신에게 묻기 시작할 수 있습니다. – matt
많은 성공을 거두었습니다. 이것이 제가 질문하는 이유입니다. 나는 'ViewController'에서'ViewController2'의 레이블 텍스트를 어떻게 설정 할 수 있습니까? – Martin
이 유형의 질문은 여기에서 가장 빈번한 신속한 질문이라고 생각합니다. – mustafa