gpt4 book ai didi

swift - 如何在 Swift 中生成一个随机的 unicode 字符?

转载 作者:行者123 更新时间:2023-11-28 11:15:17 24 4
gpt4 key购买 nike

我目前尝试创建随机 unicode 字符生成失败,并出现错误,例如我在其他问题 here 中提到的错误.这显然不是生成随机数那么简单。

问题:如何在 Swift 中生成一个随机的 unicode 字符?

最佳答案

Unicode Scalar Value

Any Unicode code point except high-surrogate and low-surrogate code points. In other words, the ranges of integers 0 to D7FF and E000 to 10FFFF inclusive.

因此,我制作了一小段代码。见下文。

这段代码有效

func randomUnicodeCharacter() -> String {
let i = arc4random_uniform(1114111)
return (i > 55295 && i < 57344) ? randomUnicodeCharacter() : String(UnicodeScalar(i))
}
randomUnicodeCharacter()

此代码有效!

let N: UInt32 = 65536
let i = arc4random_uniform(N)
var c = String(UnicodeScalar(i))
print(c, appendNewline: false)

我对 this 有点困惑和 this . [最大值:65535]

关于swift - 如何在 Swift 中生成一个随机的 unicode 字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32157453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com