Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Swift – when do we need to use 'leastNormalMagnitude', 'leastNonzeroMagnitude'

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 an Ambiguous use of 'leastNonzeroMagnitude' error from the Xcode

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading