IT

치명적인 오류 : 클래스에 구현되지 않은 초기화 프로그램 'init (coder :)'사용

lottoking 2020. 6. 7. 10:09
반응형

치명적인 오류 : 클래스에 구현되지 않은 초기화 프로그램 'init (coder :)'사용


Swift와 함께 남은 프로젝트를 계속하기로 결정했습니다. UIViewcontroller스토리 보드보기 컨트롤러에 사용자 정의 클래스 (서브 클래스 )를 추가하고 프로젝트를로드하면 앱이 갑자기 다음 오류와 함께 충돌합니다.

치명적인 오류 : 클래스에 구현되지 않은 초기화 프로그램 'init (coder :)'사용

이것은 코드입니다.

import UIKit

class TestViewController: UIViewController {

    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        // Custom initialization
    }

    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.
    }

    /*
    // #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
}

무언가를 제안하십시오


발행물

이는 init?(coder aDecoder: NSCoder)대상에 이니셜 라이저가 없기 때문입니다 UIViewController. 이 메소드는 호출 UIViewController에서 a를 인스턴스화하기 때문에 필요 UIStoryboard합니다.

우리는 초기화하는 방법을 보려면 UIViewControllerA로부터 UIStoryboard, 살펴 보시기 바랍니다 여기

Objective-C에서 이것이 왜 문제가되지 않습니까?

때문에 오브젝티브 C는 자동으로 필요한 모든 상속 UIViewController초기화합니다.

왜 Swift가 자동으로 이니셜 라이저를 상속하지 않습니까?

스위프트 는 기본적으로 안전 때문에 이니셜 라이저를 상속하지 않습니다. 그러나 모든 속성에 값이 있거나 (선택 사항) 하위 클래스가 지정된 초기화기를 정의하지 않은 경우 수퍼 클래스에서 모든 초기화자를 상속합니다.


해결책

1. 첫 번째 방법

init?(coder aDecoder: NSCoder)대상에서 수동 구현UIViewController

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

2. 두 번째 방법

init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)대상에서 제거 UIViewController하면 Dave Wood아래 답변에서 지적한 것처럼 수퍼 클래스에서 필요한 모든 초기화 프로그램이 상속 됩니다.



@ 3r1d 이외의 다른 옵션은 클래스에서 다음 init 메소드를 대신 제거하는 것입니다.

init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    // Custom initialization
}

Including that init method, prevents the sub class from inheriting the init(coder aDecoder: NSCoder!) from its super class. By not including it, your class will inherit both.

Note: See WWDC 2014 Session 403 "Intermediate Swift" at about the 33:50 mark for more details.


For people having the same issue with swift UICollectionViewCells, add the code that @3r1d suggested to your custom UICollectionViewCell class and not to the View Controller:

init(coder aDecoder: NSCoder!)
{
    super.init(coder: aDecoder)
}

For those needing the code in Swift:

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

[Edit] This was for an older version of Swift. Possibly doesn't work anymore.


I had this problem in a programmatic collectionView cell and even though the op is asking about a vc I still landed on this question when searching for an answer. For me the issue was I did have

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

implemented so the top answer didn't work. What I didn't have in the cell was the initializer:

// my programmatic cell was missing this
override init(frame: CGRect) {
    super.init(frame: frame)
}

Once I added it the error went away


Rather than adding some methods for making internal mechanism work fine, i would go with defining my attributes as @lazy and initialise them right in the class scope.

참고URL : https://stackoverflow.com/questions/24036393/fatal-error-use-of-unimplemented-initializer-initcoder-for-class

반응형