tl;dr
IBでIdentifierを設定すると死ぬので設定してはいけない
前提
バージョン
- Xcode 12.5(12E262)
- Swift 5.0
状況
フッタを以下のように設定してある
let footerRegistration = UICollectionView.SupplementaryRegistration<SomeFooterView>( supplementaryNib: UINib(nibName: "SomeFooterView", bundle: nil), elementKind: UICollectionView.elementKindSectionFooter ) { _, _, _ in } dataSource.supplementaryViewProvider = { $0.dequeueConfiguredReusableSupplementary(using: footerRegistration, for: $2) }
SomeFooterView.swift
は以下のようになっていて,
class SomeFooterView: UICollectionReusableView { }
SomeFooterView.xib
には以下のように設定されたUICollectionReusableView
が一つ置いてある
Identity Inspector
Attributes Inspector
エラーと対処法
これを走らせると以下のようなエラーを吐いて死ぬ
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'view reuse identifier in nib (SomeFooterView) does not match the identifier used to register the nib (AF8D09E3-658E-4326-A275-C15DDF32B6F0)'
ドキュメントにはこう書いてあって,
The registration occurs automatically when you pass the supplementary registration to dequeueConfiguredReusableSupplementary(using:for:).
dequeueConfiguredReusableSupplementary(using:for:)
の中でいい感じにIdentifierを設定してregisterしてくれるのでSomeFooterView.xib
でIdentifierを登録する必要はないっぽい
これを空欄にすると期待通り動く
感想
俺はただ新しい機能を使ってフッタを表示したいだけだったのに,ハマったのとこの記事を書くので日付が変わってしまった… しかも改めて記事にしてみるとめちゃくちゃしょうもない
まあいいや,おやすみ