加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

integer – Swift将UInt转换为Int

发布时间:2020-12-14 06:01:50 所属栏目:百科 来源:网络整理
导读:我有这个表达式返回一个UInt32: let randomLetterNumber = arc4random()%26 我想能够使用这个if语句中的数字: if letters.count randomLetterNumber{ var randomLetter = letters[randomLetterNumber]} 这个问题是控制台给我这个 Playground execution fai
我有这个表达式返回一个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。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读