Unity3D -- C#之InvalidOperationException: out of sync
来源:互联网 发布:詹姆斯生涯总数据 编辑:程序博客网 时间:2024/06/15 22:22
详细报错:
InvalidOperationException: out of syncSystem.Collections.Generic.Dictionary`2+Enumerator[System.String,System.Int32].VerifyState () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:912)System.Collections.Generic.Dictionary`2+Enumerator[System.String,System.Int32].MoveNext ()
定位到报错位置,是使用了字典(Dictionary),并且在遍历字典的时候更改了字典内容(删除,修改,添加)
private Dictionary<string, int> colorDict = new Dictionary<string, int> ();foreach (var item in colorDict) { if (item.Value == 1) { colorDict [item.Key] = -1; }}
由于字典存储数据的特殊性,所以我们不能再遍历的时候修改数据,这样会导致内存数据异常。所以我将以上代码实现方式更改一下,先将需要修改的内容记录下来,然后在修改。
List<string> needChangeList = new List<string> ();if (colorDict != null && colorDict.Count > 0) { foreach (var item in colorDict) { if (item.Value == 1) { needChangeList.Add (item.Key); } }}if (needChangeList.Count > 0) { foreach (var item in needChangeList) { colorDict [item] = -1; }}
当我们更改字典,数组,堆栈,链表等数据结构的数据时,我们一定要思考该数据结构存储数据的方式,然后采用对应的方法更改数据,不然会造成很多异常。
阅读全文
0 0
- Unity3D -- C#之InvalidOperationException: out of sync
- [Unity3D]InvalidOperationException: out of sync
- C#报错:InvalidOperationException: out of sync
- 【小松教你手游开发】【unity实用技能】InvalidOperationException: out of sync
- Unity在Dictionary中删除修改元素时出现InvalidOperationException: out of sync
- snapshot out of sync
- C# Dictionary修改遇到的一个错误 out of sync!
- C# 连接Mysql报错 Out of sync with server
- out-of-sync in juno
- mysql Commands out of sync
- svn_is out of sync with file system
- FragmentTabHost] File is Out of sync
- out of sync with file system
- unity 报 out of sync 解决办法
- Mysql问题一则:Commands out of sync
- resource is out of sync with the file system
- resource is out of sync with the file system
- Resource is out of sync with the file system: 解决办法。
- Zookeeper的安装与启动
- stderr和stdout详细解说
- 在CentOS7下同时安装、使用Python2.x和Python3.x
- 在C:\WINDOWS\system32>下如何进入命令提示符
- Markdown入门语法整理
- Unity3D -- C#之InvalidOperationException: out of sync
- Spring、SpringMVC与mybatis的集合的配置文档
- Python的字典的items(), keys(), values(),时间类型处理
- leaf开源服务器第四节-分析源码实现模拟TCP客户端
- Java 里如何实现线程间通信
- 从0到1 | 0基础/转行如何用3个月搞定机器学习
- 常见漏洞
- 494. Target Sum
- 六数码 广搜例题 附带一些实用的小用法