为什么Array.count不是UInt? [英] Why isn't Array.count a UInt?

查看:125
本文介绍了为什么Array.count不是UInt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 Array.count a UInt 而不是 Int



如何 Array.count 永远是否定的?

解决方案

有关Swift类型的Apple文档这里





仅当您特别需要与平台的本地词大小相同的大小的无符号整数类型时才使用UInt。如果不是这种情况,则优选Int,即使已知要存储的值是非负的。对Int整数值的一致使用有助于代码互操作性,避免了在不同数字类型之间进行转换的需要,并且匹配整数类型推理,如类型安全和类型推断中所述。



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

How could Array.count ever be negative?

解决方案

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.

这篇关于为什么Array.count不是UInt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆