swiftxcodeuicollectionviewuicollectionviewlayoutcgsize

CGSize is not accepting `.zero`. Ambiguous use of 'init(width:height:)'


In UICollectionViewDelegateFlowLayout when I am using:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 0, height: 0)
}

It works fine.

But when I am trying :

CGSize(width: .zero, height: .zero)

It shows an error as:

Ambiguous use of 'init(width:height:)'

In fact :

CGSize(width: .zero, height: 0)

also works. It just adding both as .zero is not working.


Solution

  • The syntax

    CGSize(width: .zero, height: 0)
    

    is not recommended anyway even if it works.


    .zero is a static variable of CGSize and can be used properly

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.zero
    }
    

    or even

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return .zero
    }