integer – Swift将UInt转换为Int

我有这个表达式返回一个UInt32:
let randomLetterNumber = arc4random()%26

我想能够使用这个if语句中的数字:

if letters.count > randomLetterNumber{
    var randomLetter = letters[randomLetterNumber]
}

这个问题是控制台给我这个

Playground execution Failed: error: <REPL>:11:18: error: Could not find an overload for '>' that accepts the supplied arguments
if letters.count > randomLetterNumber{
   ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~

问题是UInt32不能与Int比较。我想把randomLetterNumber转换为Int。我努力了:

let randomLetterUNumber : Int = arc4random()%26
let randomLetterUNumber = arc4random()%26 as Int

这两个原因都无法找到接受提供的参数的’%’的重载。

如何转换值或在if语句中使用它?

Int(arc4random_uniform(26))做两件事,一个它消除了你当前的方法的负面结果,第二个应该从结果中正确地创建一个Int。

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...