ぜのぜ

しりとりしようぜのぜのぜのぜ

dequeueConfiguredReusableSupplementary(using:for:)の罠っぽいの踏んだ

tl;dr

IBでIdentifierを設定すると死ぬので設定してはいけない

github

前提

バージョン

  • 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 f:id:gurrium:20210502000708p:plain

Attributes Inspector f:id:gurrium:20210502000819p:plain

エラーと対処法

これを走らせると以下のようなエラーを吐いて死ぬ

*** 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を登録する必要はないっぽい

これを空欄にすると期待通り動く f:id:gurrium:20210502003630p:plain

感想

俺はただ新しい機能を使ってフッタを表示したいだけだったのに,ハマったのとこの記事を書くので日付が変わってしまった… しかも改めて記事にしてみるとめちゃくちゃしょうもない

まあいいや,おやすみ