我目前尝试创建随机 unicode 字符生成失败,并出现错误,例如我在其他问题 here 中提到的错误.这显然不是生成随机数那么简单。
问题:如何在 Swift 中生成一个随机的 unicode 字符?
最佳答案
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)
关于swift - 如何在 Swift 中生成一个随机的 unicode 字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32157453/