[ 주의점 ]
[ 문제 상황 ]
//곡의 점수는 있지만 MusicDataDic에는 없는 경우(해당 곡이 빠진 경우)를 확인
foreach (var musicData in totalScoreData.totalScoreDic.Keys)
{
if (SoundManager.instance.MusicDataDic.ContainsKey(musicData) == false)
{
Debug.Log("키는" + musicData);
//해당 데이터를 지워준다.
totalScoreData.totalScoreDic.Remove(musicData);
}
}
=>문제의 메서드는 다음과 같다 . 아래와 같은 오류를 보여주었다 .
[ 해결 ]
=>이는 foeach 문을 돌리는 중 리스트 내부의 요소를 삭제하였기때문에 생긴 문제이다 .
foreach 문을 쓰는 대신 for문을 + 뒤 요소부터 검사하여 문제를 해결하였다
//곡의 점수는 있지만 MusicDataDic에는 없는 경우(해당 곡이 빠진 경우)를 확인
List<string> list = totalScoreData.totalScoreDic.Keys.ToList();
for (int i=list.Count-1;i >=0;i--)
{
if (SoundManager.instance.MusicDataDic.ContainsKey(list[i]) == false)
{
Debug.Log("키는" + list[i]);
//해당 데이터를 지워준다.
totalScoreData.totalScoreDic.Remove(list[i]);
}
}
출처
https://ruen346.tistory.com/109?category=814615
'C#' 카테고리의 다른 글
[ 이것이 C#이다 ] Chapter 17 . dynamic 형식 (0) | 2024.04.05 |
---|---|
[ C# ]Conditional Attribute (0) | 2023.06.26 |
[c#] 배열을 딕셔너리로 만들고 인덱스에 따라 정렬하여 사용하기 (0) | 2023.04.04 |
[C#]sealed 한정자 (0) | 2023.03.02 |
[ C# ] Enum 결합하여 사용하기 (0) | 2023.02.22 |