Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

potato's iOS Story/๊ฐœ๋ฐœํ•˜๋ฉด์„œ ๋งŒ๋‚œ ์นœ๊ตฌ๋“ค

[Error] fatalError("Expected superview but found nil when attempting make constraint `equalToSuperview`.")

๊ฐ์ž ๐Ÿฅ” 2022. 7. 13. 17:28
๋ฐ˜์‘ํ˜•

์•ฑ์„ ๋งŒ๋“ค๊ณ  ์žˆ๋Š”๋ฐ ๊ฐ‘์ž๊ธฐ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ.

 

์ธํ„ฐ๋„ท์„ ์ฐพ์•„๋ณด๋‹ˆ๊นŒ addsubView๋ฅผ ์•ˆํ•ด์„œ ์ƒ๊ธฐ๋Š” ๋ฌธ์ œ์ธ๊ฒƒ ๊ฐ™์€๋ฐ, ๋‹ค์‹œ ์ฐจ๊ทผ์ฐจ๊ทผ ์ฝ”๋“œ๋ฅผ ์ฝ์–ด๋ณด๋ฉด์„œ ํ•ด๊ฒฐํ•ด๋ณด์ž. 
๋„๋Œ€์ฒด ์–ด๋””์„œ ๋ฌธ์ œ๊ฐ€ ๋‚˜๋Š”๊ฒƒ์ธ๊ฐ€? (์•„๋ฌด๋ฆฌ ๋ด๋„ addsubview์•ˆํ•œ ๋ถ€๋ถ„์ด ์—†์Œ.. ์•„๋ฌด๋ž˜๋„ add๋Š” ๋‹ค ํ•ด์คฌ์ง€๋งŒ ๊ทธ ์‚ฌ์ด์— ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋’ค๋ฐ”๋€Œ๊ฑฐ๋‚˜ ๋“ฑ ์ถฉ๋Œ์ด ์ผ์–ด๋‚˜๋Š” ๊ฒƒ..๊ฐ™..ใ…‡.ใ…ก.ใ„ด..ใ….... ใ… )

 

๐Ÿ”ต ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋ฒ„ํŠผ์„ ๋„ฃ์–ด์ฃผ์–ด์•ผํ•ด์„œ switch๋ฌธ์„ ์ด์šฉํ•ด์„œ func์„ ๋„ฃ์–ด์คŒ

extension HomeCellListCell {
    func setupViews() {
        [listImageView, subTitle, title].forEach { addSubview($0) }
        listImageView.snp.makeConstraints {
            $0.leading.trailing.equalToSuperview().inset(24.0)
            $0.width.height.equalTo(40.0)
            $0.top.equalToSuperview().inset(20.0)
            $0.trailing.equalToSuperview().inset(20.0)
        }
        subTitle.snp.makeConstraints {
            $0.leading.equalTo(listImageView.snp.trailing).offset(24.0)
            $0.top.equalTo(listImageView.snp.top)
            $0.trailing.equalToSuperview()
        }
        title.snp.makeConstraints {
            $0.top.equalTo(subTitle.snp.bottom).offset(5.0)
            $0.leading.equalTo(subTitle.snp.leading)
            $0.bottom.equalTo(listImageView.snp.bottom)
            $0.trailing.equalToSuperview()
        }
        
        switch cellRow?.cellType {
        case .account:
            setupSendButton()
        case .history:
            setupHistoryButton()
        default:
            return
        }
    }
    
    func setupSendButton() {
        addSubview(sendButton)
        sendButton.snp.makeConstraints {
            $0.trailing.equalToSuperview().inset(24.0)
            $0.centerY.equalTo(snp.centerY)
            $0.width.equalTo(50.0)
            $0.height.equalTo(30.0)
        }
    }
    
    func setupHistoryButton() {
        addSubview(historyButton)
        sendButton.snp.makeConstraints {
            $0.trailing.equalToSuperview().inset(24.0)
            $0.centerY.equalTo(snp.centerY)
            $0.width.equalTo(50.0)
            $0.height.equalTo(30.0)
        }
    }
}

์ด๋ ‡๊ฒŒ ๋„ฃ์–ด์ฃผ๊ณ , setupViews() ๋ฉ”์„œ๋“œ๋ฅผ final class์˜ init์— ๋„ฃ์–ด์ฃผ๋ฉด์„œ addsubview๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค.

   func setup(row: HomeCellListData) {
        cellRow = row
        setupViews()
    }

์ € switch ๋ฌธ์„ ์ฃผ์„์ฒ˜๋ฆฌํ•˜๊ณ  ํ•˜๋ฉด, ์ •์ƒ์ ์œผ๋กœ ๋Œ์•„๊ฐ€๋Š”๋ฐ switch ๋ฌธ์ด ๋“ค์–ด๊ฐ€๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค. 

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‘๋ฒˆ์žฌ ์ฝ”๋“œ์—์„œ cellRow = row ์ฝ”๋“œ๋ฅผ ๋„ฃ๋Š” ์ˆœ๊ฐ„๋ถ€ํ„ฐ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค. ๊ทธ๋‹ˆ๊นŒ ์—ฌ๊ธฐ์„œ ๋ฌด์–ธ๊ฐ€ ์ถฉ๋Œ์ด ์ƒ๊ธฐ๋Š”๊ฑฐ ๊ฐ™์Œ.. ๊ทธ๋‹ˆ๊นŒ ์ด๊ฒŒ๋ญ๋ƒ๊ณ !!

 

๐Ÿ”ต ์ฝ˜์†”์ฐฝ์„ ์ž์„ธํžˆ ๋šซ์–ด์ ธ๋ผ ์ณ๋‹ค๋ด๋ณด์ž

์˜ค์ผ€์ด ์ผ๋‹จ ์ด ํŒŒ์ผ์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๊ฑฐ ๋งž๊ณ . 

๋ญ”๊ฐ€ ํ•ด๋‹น ํŒŒ์ผ line 105 ์—์„œ ์—๋Ÿฌ๊ฐ€ ๋‚œ๊ฑฐ๊ฐ™์€๋ฐ, 

๋ญ”๊ฐ€ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š”๋ฐ ๋ ˆ์ด์•„์›ƒ์„ ๋‹จ๋‹จํžˆ ์ž˜๋ชป์„ค์ •ํ–ˆ๊ตฌ๋‚˜..! ๊นŒ์ง€ ๊ฐ์„ ์žก์„ ์ˆ˜ ์žˆ์—ˆ์›€.

๊ทธ๋ž˜์„œ ์ € ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚ค๋Š” ๋ ˆ์ด์•„์›ƒ์„ ์ „๋ถ€ ์ฃผ์„์ฒ˜๋ฆฌํ•˜๊ณ  ๋Œ๋ ค๋ณด๋‹ˆ๊นŒ

์ด๋ ‡๊ฒŒ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๋ฒ„ํŠผ๋“ค์ด ์›ํ•˜๋Š” cell ์•ˆ์— ๋“ค์–ด๊ฐ„๋‹ค!

 

๐Ÿ”ต ๊ฒฐ๋ก 

โš ๏ธ fatalError("Expected superview but found nil when attempting make constraint `equalToSuperview`.")

์ด ์—๋Ÿฌ๋Š”, ๋ ˆ์ด์•„์›ƒ์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒจ์„œ ๋ทฐ๊ฐ€ ๋กœ๋“œ๋˜์ง€ ์•Š์•„์„œ ์ƒ๊ธด๋ฌธ์ œ์˜€๋‹ค.
์ธํ„ฐ๋„ท์„ ์ฐพ์•„๋ณด๋‹ˆ๊นŒ ๋Œ€๋ถ€๋ถ„ ํ•ด๋‹น ์—๋Ÿฌ๋Š” addSubview๋ฅผ ํ•˜์ง€ ์•Š์•„์„œ ์ƒ๊ธฐ๋Š” ๋ฌธ์ œ๋ผ๊ณ  ํ•˜๋‹ˆ๊นŒ, ์ด ์—๋Ÿฌ๊ฐ€ ์ƒ๊ธฐ๋ฉด addSubview๋ฅผ ์ œ๋Œ€๋กœ ํ–ˆ๋Š”์ง€, ๊ทธ๋ฆฌ๊ณ  ๋‚ด๊ฐ€ ์„ค์ •ํ•œ ๋ ˆ์ด์•„์›ƒ์— ๋ฌธ์ œ๊ฐ€ ์—†๋Š”์ง€ ๋จผ์ € ์‚ดํŽด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

๋ฐ˜์‘ํ˜•