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
String 飞镖串可伸缩接口_String_Dart_Iteration_Iterable - Fatal编程技术网

String 飞镖串可伸缩接口

String 飞镖串可伸缩接口,string,dart,iteration,iterable,String,Dart,Iteration,Iterable,为什么Dart String类不实现iterable接口?字符串似乎有一个明显的迭代概念,即只需依次返回每个字符。可能没有您希望的那么简洁 String s='foo'; s、 codeUnits.forEach((f)=>print(新字符串.fromCharCode(f)); s、 runes.forEach((f)=>print(新字符串.fromCharCode(f)); 回答“为什么”: Iterable接口是一个相当重的接口(许多成员),用于元素集合 虽然字符串可以看作是字符的列表(

为什么Dart String类不实现iterable接口?字符串似乎有一个明显的迭代概念,即只需依次返回每个字符。

可能没有您希望的那么简洁

String s='foo';
s、 codeUnits.forEach((f)=>print(新字符串.fromCharCode(f));
s、 runes.forEach((f)=>print(新字符串.fromCharCode(f));
回答“为什么”:

Iterable
接口是一个相当重的接口(许多成员),用于元素集合

虽然
字符串
可以看作是字符的
列表
(但Dart没有字符类型,因此它实际上是“单个字符串列表”),但这并不是它的主要用途,而且作为
可编辑的
也会扰乱实际的字符串方法

更好的解决方案是使用
列表
查看
字符串
。这是相当容易做到的:

导入“省道:集合”;
类StringListView使用扩展ListBase{
最终字符串_字符串;
StringListView(此._字符串);
字符串运算符[](int-index)=>u字符串[index];
int get length=>_string.length;
void set length(int newLength){throw new unsupportderror(“不可修改”);}
void运算符[]=(int索引,字符串v){抛出新的不支持错误(“不可修改”);}
}
如果库公开了用于生成UnmodifiableListView的
UnmodifiableListBase
,则会更容易