如何在Swift中生成一个随机的unicode字符? [英] How can I generate a random unicode character in Swift?

查看:146
本文介绍了如何在Swift中生成一个随机的unicode字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前尝试创建一个随机的unicode字符生成失败,如错误,如我在其他问题这里

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

解决方案

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)

我对有点困惑此。 [最大值:65535]


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?

解决方案

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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆