0

swift初学者です。
チュートリアルを参考にSNSアプリを制作しています。
xcodeのバージョンは12.0.1です。
シミュレーターを立ち上げ、HomeViewControllerからProfileViewControllerへ画面遷移しようとするとエラーになります。
storyboardは使用していますが、画面遷移とタブのみで@IBOutletなどは配置していません。

以下コンソールと疑わしき箇所の内容です。

2020-10-19 10:16:30.898034+0900 gyosufan[6964:470885] [Firebase/Crashlytics] Version 4.3.1
2020-10-19 10:16:30.900738+0900 gyosufan[6964:471133] 6.28.0 - [Firebase/Analytics][I-ACS023007] Analytics v.60602000 started
2020-10-19 10:16:30.904768+0900 gyosufan[6964:471133] 6.28.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see http://goo.gl/RfcP7r)
2020-10-19 10:16:30.936124+0900 gyosufan[6964:471137] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed
2020-10-19 10:16:30.950352+0900 gyosufan[6964:471128] 6.28.0 - [Firebase/Analytics][I-ACS031025] Analytics screen reporting is enabled. Call +[FIRAnalytics setScreenName:setScreenClass:] to set the screen name or override the default screen class name. To disable screen reporting, set the flag FirebaseScreenReportingEnabled to NO (boolean) in the Info.plist
2020-10-19 10:16:30.972012+0900 gyosufan[6964:471134] 6.28.0 - [Firebase/Analytics][I-ACS800023] No pending snapshot to activate. SDK name: app_measurement
2020-10-19 10:16:31.041049+0900 gyosufan[6964:471144] 6.28.0 - [Firebase/Analytics][I-ACS023012] Analytics collection enabled
2020-10-19 10:16:31.257939+0900 gyosufan[6964:471144] 6.28.0 - [Firebase/Crashlytics][I-CLS000000] Failed to download settings Error Domain=FIRCLSNetworkError Code=-5 "(null)" UserInfo={status_code=404, type=2, request_id=, content_type=text/html; charset=utf-8}
Could not cast value of type 'gyosufan.ProfileTabsCollectionReusableView' (0x10145b908) to 'gyosufan.ProfileInfoHeaderCollectionReusableView' (0x10145bca0).
2020-10-19 10:16:33.573986+0900 gyosufan[6964:470885] Could not cast value of type 'gyosufan.ProfileTabsCollectionReusableView' (0x10145b908) to 'gyosufan.ProfileInfoHeaderCollectionReusableView' (0x10145bca0).
Could not cast value of type 'gyosufan.ProfileTabsCollectionReusableView' (0x10145b908) to 'gyosufan.ProfileInfoHeaderCollectionReusableView' (0x10145bca0).
CoreSimulator 732.17 - Device: iPhone SE (2nd generation) (52F44A5A-DA3B-4922-8049-5BE63803AA59) - Runtime: iOS 14.0 (18A372) - DeviceType: iPhone SE (2nd generation)
(lldb) 
import UIKit

//profile view controller
final class ProfileViewController: UIViewController {
    
    private var collectionView: UICollectionView?

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        configureNavigationBar()
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 1
        layout.minimumInteritemSpacing = 1
        layout.sectionInset = UIEdgeInsets(top: 0, left: 1, bottom: 0, right: 1)
        let size = (view.width - 4)/3
        layout.itemSize = CGSize(width: size, height: size)
        collectionView = UICollectionView(frame: .zero,
                                          collectionViewLayout: layout)
        collectionView?.backgroundColor = .red
        
        //cell
        collectionView?.register(PhotoCollectionViewCell.self,
                                 forCellWithReuseIdentifier: PhotoCollectionViewCell.identifier)
        
        //header
        collectionView?.register(ProfileInfoHeaderCollectionReusableView.self,
                                 forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: ProfileInfoHeaderCollectionReusableView.identifier)
        collectionView?.register(ProfileTabsCollectionReusableView.self,
                                 forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: ProfileTabsCollectionReusableView.identifier)
        
        collectionView?.delegate = self
        collectionView?.dataSource = self
        guard let collectionView = collectionView else {
            return
        }
        view.addSubview(collectionView)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        collectionView?.frame = view.bounds
    }
    
    private func configureNavigationBar() {
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "gear"),
                                                            style: .done,
                                                            target: self,
                                                            action: #selector(didTapSettingButton))
    }
    
    @objc private func didTapSettingButton() {
        let vc = SettingViewController()
        vc.title = "設定"
        navigationController?.pushViewController(vc, animated: true)
    }
}

extension ProfileViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return 0
        }
        return 30
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PhotoCollectionViewCell.identifier, for: indexPath) as! PhotoCollectionViewCell
        
        cell.configure(debug: "test")
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        guard kind == UICollectionView.elementKindSectionHeader else {
            //footer
            return UICollectionReusableView()
        }
        
        if indexPath.section == 1 {
            //header
            let tabControllHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
                                                                         withReuseIdentifier: ProfileTabsCollectionReusableView.identifier,
                                                                         for: indexPath) as! ProfileTabsCollectionReusableView
            return tabControllHeader
        }
        
        let profileHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
                                                                     withReuseIdentifier: ProfileInfoHeaderCollectionReusableView.identifier,
                                                                     for: indexPath) as! ProfileInfoHeaderCollectionReusableView
        return profileHeader
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if section == 0 {
            return CGSize(width: collectionView.width,
                          height: collectionView.height/3)
        }
        
        //タブのサイズ
        return CGSize(width: collectionView.width,
                      height: 65)
    }
}

let profileHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
                                                                     withReuseIdentifier: ProfileInfoHeaderCollectionReusableView.identifier,
                                                                     for: indexPath) as! ProfileInfoHeaderCollectionReusableView
        return profileHeader

ここの as の部分に

Thread 1: signal SIGABRT

とエラーが出ます。

import FirebaseAuth
import UIKit

class HomeViewController: UIViewController {
    
    private let tableView: UITableView = {
        let tableView = UITableView()
        tableView.register(IGFeedPostTableViewCell.self,
                           forCellReuseIdentifier: IGFeedPostTableViewCell.identifier)
        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        tableView.frame = view.bounds
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        handleNotAuthenticated()
        
    }
    
    private func handleNotAuthenticated() {
        //認証ステータスの確認
        if Auth.auth().currentUser == nil {
            
            //ログインを表示
            let loginVC = LoginViewController()
            loginVC.modalPresentationStyle = .fullScreen
            present(loginVC, animated: false)
            
        }
    }
}
//セクション数0
extension HomeViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 0
    }
    //セル数0
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    //セルを生成
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: IGFeedPostTableViewCell.identifier, for: indexPath) as! IGFeedPostTableViewCell
        return cell
    }
}

import UIKit

class ProfileInfoHeaderCollectionReusableView: UICollectionReusableView {
      static let identifier = "ProfileInfoHeaderCollectionReusableView"
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .systemBlue
        clipsToBounds = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
import UIKit

class ProfileTabsCollectionReusableView: UICollectionReusableView {
    static let identifier = "ProfileInfoHeaderCollectionReusableView"
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .orange
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

同じエラーで検索しましたが自力では解決できず、助言いただければ幸いです。

2

1 件の回答 1

0

ProfileInfoHeaderCollectionReusableViewidentifierProfileTabsCollectionReusableViewidentifierが同じ値になってしまっています。

class ProfileInfoHeaderCollectionReusableView: UICollectionReusableView {
      static let identifier = "ProfileInfoHeaderCollectionReusableView"

    //...
}
class ProfileTabsCollectionReusableView: UICollectionReusableView {
    static let identifier = "ProfileInfoHeaderCollectionReusableView" //<-
    
    //...
}

このためiOSがProfileInfoHeaderCollectionReusableViewを生成できません。
(後から設定した

collectionView?.register(ProfileTabsCollectionReusableView.self,
                         forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
                         withReuseIdentifier: ProfileTabsCollectionReusableView.identifier)

ProfileInfoHeaderCollectionReusableViewに関する設定を上書きしてしまっている。)
正しいreuseIdentifierを設定してみてください。

class ProfileTabsCollectionReusableView: UICollectionReusableView {
    static let identifier = "ProfileTabsCollectionReusableView" //<-
    
    //...
}
1
  • 回答ありがとうございます!記述ミスでした。。
    – uskitm
    2020年10月19日 6:56

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。