Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将unicodeScalars索引转换为swift字符串中的字符索引_Swift_String_Parsing_Nstextview - Fatal编程技术网

将unicodeScalars索引转换为swift字符串中的字符索引

将unicodeScalars索引转换为swift字符串中的字符索引,swift,string,parsing,nstextview,Swift,String,Parsing,Nstextview,我有一个解析器,它使用字符串的unicodeScalars视图来计算索引。从解析器的结果。 我有一个NSTextView,我想根据解析器的结果在其中放置插入符号。不幸的是,NSTextView的selectedRange需要的是字符索引,而不是unicodeScalar索引 如何将索引从Unicodescalar转换为字符?不同的字符串视图共享一个通用的字符串。索引,而NSRange(u:in:)可用于将字符串范围转换为NSRange(这是selectedRange需要的)。例如: let st

我有一个解析器,它使用字符串的unicodeScalars视图来计算索引。从解析器的结果。 我有一个
NSTextView
,我想根据解析器的结果在其中放置插入符号。不幸的是,NSTextView的
selectedRange
需要的是字符索引,而不是unicodeScalar索引


如何将索引从Unicodescalar转换为字符?

不同的字符串视图共享一个通用的
字符串。索引
,而
NSRange(u:in:)
可用于将
字符串
范围转换为
NSRange
(这是
selectedRange
需要的)。例如:


let str=“我不知道这个NSRange功能!回答太棒了。谢谢!我得到
无法使用类型为”(ClosedRange,in:String)的参数列表调用类型为“init(u:in:)”的初始值设定项“
在创建range@AP.:在发布之前,我在我的Xcode 10.2中测试了上述代码,它编译和运行没有问题。您使用的是哪个版本的Swift?@AP:我用Swift 5(Xcode 10.2)和Swift 4.2(Xcode 10.1)测试了它