When do we need to use 'leastNormalMagnitude', 'leastNonzeroMagnitude'?

45 views Asked by At

I am developing a collection view, In some cases, to avoid runtime exceptions, I need to return my collection view size to a least minimum size. So that the view takes a minimum space which is too small to view.

Here is the code snippet I tried but I am getting the error:

Ambiguous use of 'leastNonzeroMagnitude'

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, estimatedItemSize section: Int) -> CGSize {
   return .init(width: .leastNormalMagnitude, height: .leastNonzeroMagnitude)
}

But if I return .init(width: 0.0, height: .leastNonzeroMagnitude) there is no error.

I found a similar question What is the difference between leastNormalMagnitude, leastNonZeroMagnitude and automaticDimention in UITableview. But there is no answer to this question.

1

There are 1 answers

0
son On BEST ANSWER

According to the documentation about leastNonzeroMagnitude

This value compares less than or equal to all positive numbers, but greater than zero. If the type supports subnormal values, leastNonzeroMagnitude is smaller than leastNormalMagnitude; otherwise they are equal.

So simply, leastNonzeroMagnitude <= leastNormalMagnitude and in this scenario, it will make no difference.


Both leastNonzeroMagnitude and leastNormalMagnitude have static variables that return a Double | CGFloat | Float. The CGSize initial function cannot determine which value it may be from the above types. That's why the error appears. You can click the error and check the Quick Help menu on the right hand side of Xcode's navigation.

Ambiguous use of 'leastNonzeroMagnitude'

enter image description here

To fix it, you just need to make leastNormalMagnitude and leastNonzeroMagnitude conform to the correct type before returning CGSize

let width: Double = .leastNormalMagnitude
let height: Double = .leastNonzeroMagnitude
return .init(width: width, height: height)