property of UIContentConfiguration cannot be called

Code Block private func setupCollectionView() {
    let registration = UICollectionView.CellRegistration<UICollectionViewListCell, Article> { cell, _, item in
      var content = cell.defaultContentConfiguration()
      content.text = "title"
      cell.contentConfiguration = content
    }

Code Block
XCTAssertEqual(cell.contentConfiguration.text, "title")

but


Value of type 'UIContentConfiguration?' has no member 'text'

Why does 'UIContentConfiguration?' have no member 'text' ??
Answered by TaiseiHikawa in 675748022
Thank you for your answers!
I missed the difference UIContentConfiguration and UIListContentConfiguration.
I solved by rewriting as follows
Code Block
guard let content = cell.contentConfiguration as? UIListContentConfiguration else {
      XCTFail()
      return
    }
    XCTAssertEqual(content.text, "title")


Looks as if content was optional ('UIContentConfiguration?'), which is surprising, but.

Could you test:
Code Block
let registration = UICollectionView.CellRegistration<UICollectionViewListCell, Article> { cell, _, item in
if var content = cell.defaultContentConfiguration() {
content.text = "title"
cell.contentConfiguration = content
}
}


Something strange on this forum.

The abstract of your question is:
private func setupCollectionView() {    let registration = UICollectionView.CellRegistrationUICollectionViewListCell, Article { cell, _, item in      var content = cell.defaultContentConfiguration()      content.text = "title" 

And the first line
Code Block
private func setupCollectionView() {

doesn't show in the post itself !

Now back to your question.

I tested your code (which is exactly what is proposed in documentation), and initially I got content type as UIContentConfiguration (which is a protocol) not UIListContentConfiguration (which is a struct with text property).

Could you check the type of content ? Error message seems to show it is UIContentConfiguration

So you could also test
Code Block
let registration = UICollectionView.CellRegistration<UICollectionViewListCell, Article> { cell, _, item in
if var content = cell.defaultContentConfiguration() as? UIListContentConfiguration {
content.text = "title"
cell.contentConfiguration = content
}
}

Note: I tested the following
Code Block
let registration = UICollectionView.CellRegistration<UICollectionViewListCell, Int> { cell, _, item in
var content = cell.defaultContentConfiguration() as? UIContentConfiguration
content.text = "title"
cell.contentConfiguration = content
}

and got the exact same error message as yours :

Value of type 'UIContentConfiguration?' has no member 'text'.

That seems to confirm the problem.
Accepted Answer
Thank you for your answers!
I missed the difference UIContentConfiguration and UIListContentConfiguration.
I solved by rewriting as follows
Code Block
guard let content = cell.contentConfiguration as? UIListContentConfiguration else {
      XCTFail()
      return
    }
    XCTAssertEqual(content.text, "title")


property of UIContentConfiguration cannot be called
 
 
Q