如何在Swift中生成一个随机的unicode字符? [英] How can I generate a random unicode character in Swift?
问题描述
我目前尝试创建一个随机的unicode字符生成失败,如错误,如我在其他问题这里。
如何在Swift中生成一个随机的unicode字符?
任何Unicode代码点(高代理和低代理代码
除外)。换句话说,整数范围为0到D7FF和E000
到10FFFF(含)。
一个小代码的代码段。
此代码的工作原理
func randomUnicodeCharacter > String {
let i = arc4random_uniform(1114111)
return(i> 55295&& i< 57344)? randomUnicodeCharacter():String(UnicodeScalar(i))
}
randomUnicodeCharacter()
< h3>此代码 不 工作!
UInt32 = 65536
let i = arc4random_uniform(N)
var c = String(UnicodeScalar(i))
print(c,appendNewline:false)
My current attempts at creating a random unicode character generate have failed with errors such as those mentioned in my other question here. It's obviously not as simple as just generating a random number.
Question: How can I generate a random unicode character in Swift?
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.
So, I've made a small code's snippet. See below.
This code works
func randomUnicodeCharacter() -> String {
let i = arc4random_uniform(1114111)
return (i > 55295 && i < 57344) ? randomUnicodeCharacter() : String(UnicodeScalar(i))
}
randomUnicodeCharacter()
This code doesn't work!
let N: UInt32 = 65536
let i = arc4random_uniform(N)
var c = String(UnicodeScalar(i))
print(c, appendNewline: false)
I was a little bit confused with this and this. [Maximum value: 65535]
这篇关于如何在Swift中生成一个随机的unicode字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!