本文介绍了为什么Swift语言指南建议使用Int“即使值已知为非负数”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这是关于Swift中编程风格的问题,特别是 Int vs UInt 。 Swift编程语言指南建议程序员使用通用的有符号整数类型 Int ,即使变量已知为非负数。从指南:但是, UInt 在32位体系结构上为32位无符号,64位体系结构上为64位无符号,因此有使用 Int 超过 UInt 没有性能优势。 相反,Swift指南给出了后面的例子:这里,如果代码写成: ,则可能会在 let age:UInt = -3 //这会导致编译器错误,因为-3是负数 还有很多其他情况(例如任何将索引集合的地方),其中使用 UInt 将在编译时而不是运行时捕获问题。 所以问题:是Swift编程语言指南中的建议,并且使用 Int 即使当要存储的值已知为非负数超过使用 UInt ? > 其他注意事项:使用Swift几个星期后,我们清楚地认识到与 UInt 是必需的。例如 AVFoundation 框架在需要计数(样本数/帧/通道等)的任何地方使用无符号整数。将这些值转换为 Int 可能导致严重错误,其中值大于 Int.max 解决方案我不认为使用UInt是安全的,因为你认为它。如您所述: let age:UInt = -3 导致编译器错误。我也试过: let myAge:Int = 1 let age:UInt = UInt(myAge) - 3 这也导致编译器错误。但是下面的(在我看来在实际程序中更常见的)方案没有编译器错误,但实际上导致运行时错误 EXC_BAD_INSTRUCTION : func sub10(num:Int) - > UInt { return UInt(num-10)//运行时错误,当num } sub10(4) 以及: class A { var aboveZero:UInt init(){aboveZero = 1} } let a = A() a.aboveZero = a.aboveZero - 10 //运行时错误 如果这些是简单的 Int s,而不是崩溃,您可以添加代码来检查您的条件: 如果a.aboveZero> 0 { //做你的事情} else { //处理坏数据} 我甚至可以把他们的建议等同于使用 UInt 的建议,反对使用隐式解包的可选项:除非你确定你不会得到任何负面,否则你会得到运行时错误(除了最简单的情况)。 This is a question about programming style in Swift, specifically Int vs UInt.The Swift Programming Language Guide advises programmers to use the generic signed integer type Int even when variables are known to be non-negative. From the guide:However, UInt will be 32-bit unsigned on 32-bit architectures and 64-bit unsigned on 64-bit architectures so there is no performance benefit to using Int over UInt.By contrast, the Swift guide gives a later example:Here, a runtime issue could be caught at compile time if the code had been written as:let age:UInt = -3 // this causes a compiler error because -3 is negativeThere are many other cases (for example anything that will index a collection) where using a UInt would catch issues at compile time rather than runtime.So the question: is the advice in the Swift Programming Language guide sound, and do the benefits of using Int "even when the values to be stored are known to be non-negative" outweigh the safety advantages of using UInt?Additional note: Having used Swift for a couple of weeks now its clear that for interoperability with Cocoa UInt is required. For example the AVFoundation framework uses unsigned integers anywhere a "count" is required (number of samples / frames / channels etc). Converting these values to Int could lead to serious bugs where values are greater than Int.max 解决方案 I don't think using UInt is as safe as you think it is. As you noted:let age:UInt = -3results in a compiler error. I also tried:let myAge:Int = 1let age:UInt = UInt(myAge) - 3which also resulted in a compiler error. However the following (in my opinion much more common in real programs) scenarios had no compiler error, but actually resulted in runtime errors of EXC_BAD_INSTRUCTION:func sub10(num: Int) -> UInt { return UInt(num - 10) //Runtime error when num < 10}sub10(4)as well as:class A { var aboveZero:UInt init() { aboveZero = 1 }}let a = A()a.aboveZero = a.aboveZero - 10 //Runtime errorHad these been plain Ints, instead of crashing, you could add code to check your conditions:if a.aboveZero > 0 { //Do your thing} else { //Handle bad data}I might even go so far as to equate their advice against using UInts to their advice against using implicitly unwrapped optionals: Don't do it unless you are certain you won't get any negatives, because otherwise you'll get runtime errors (except in the simplest of cases). 这篇关于为什么Swift语言指南建议使用Int“即使值已知为非负数”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-23 10:38