11

Why isn't Array.count a UInt instead of an Int?

How could Array.count ever be negative?

ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • 1
    Related: [Why does the Swift language guide suggest using Int “even when values are known to be non-negative”?](http://stackoverflow.com/questions/24180630/why-does-the-swift-language-guide-suggest-using-int-even-when-values-are-known) – Martin R Jun 23 '16 at 18:36
  • Example for an easy pitfall if you work with unsigned integers: http://stackoverflow.com/questions/37928520/swift-unexpected-error-in-simple-if-statement. – Martin R Jun 23 '16 at 18:37

1 Answers1

11

From Apple's documentation on Swift types here:

NOTE

Use UInt only when you specifically need an unsigned integer type with the same size as the platform’s native word size. If this is not the case, Int is preferred, even when the values to be stored are known to be non-negative. A consistent use of Int for integer values aids code interoperability, avoids the need to convert between different number types, and matches integer type inference, as described in Type Safety and Type Inference.

Hayden McCabe
  • 494
  • 2
  • 14