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

string – 在Swift中转换Character和CodePoint

发布时间:2020-12-14 04:44:55 所属栏目:百科 来源:网络整理
导读:我可以直接在 Swift字符和它的Unicode数值之间转换吗?那是: var i:Int = ... // A plain integer index.var myCodeUnit:UInt16 = myString.utf16[i]// Would like to say myChar = myCodeUnit as Character,or equivalent. 要么… var j:String.Index = ..
我可以直接在 Swift字符和它的Unicode数值之间转换吗?那是:

var i:Int = ...  // A plain integer index.
var myCodeUnit:UInt16 = myString.utf16[i]
// Would like to say myChar = myCodeUnit as Character,or equivalent.

要么…

var j:String.Index = ... // NOT an integer!
var myChar:Character = myString[j]
// Would like to say myCodeUnit = myChar as UInt16

我可以说:

myCodeUnit = String(myChar).utf16[0]

但这意味着为每个角色创建一个新的String.而且我这样做了数千次(解析文本),这是很多新的字符串被立即丢弃.

解决方法

Character类型表示“Unicode字形集群”,可以是多个Unicode代码点.如果需要一个Unicode代码点,则应使用UnicodeScalar类型.

(编辑:李大同)

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

    推荐文章
      热点阅读