gpt4 book ai didi

c# - key 不存在错误

转载 作者:行者123 更新时间:2023-11-30 17:30:25 25 4
gpt4 key购买 nike

根据我的理解,评论中解释了这里应该发生的事情,但由于某种原因,我在编辑器中遇到了这个错误

KeyNotFoundException: The given key was not present in the dictionary. System.Collections.Generic.Dictionary`2[System.Int32,System.String].get_Item (Int32 key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150) UpgradeManager.ScanForAvailableUpgrades () (at Assets/Scripts/UpgradeManager.cs:125 UpgradeManager.Update () (at Assets/Scripts/UpgradeManager.cs:70)

错误发生在函数 DisableSameType 被调用之后

public string GetUpgradeType(string upgradeName){
return upgradeTypes [upgradeName];
}

public void DisableSameType (string upgradeName){
string upgradeType = GetUpgradeType (upgradeName);

string[] keys = KeyByValue (upgradeTypes, upgradeType); //returns all the keys of the same type

if (keys.Length == 0) { //if there are no keys
return; //then return
} else {
for (int i = 0; i < keys.Length; i++) { //loop through all the keys
upgradesOwned [keys [i]] = false; //Set them to false
}
}
}

还有其他相关代码

public List<string> AvailableUpgrades = new List<string>();

protected Dictionary<int,string> upgradeNames = new Dictionary<int, string>();

protected Dictionary<string,bool> upgradesOwned = new Dictionary<string, bool>();
protected Dictionary<string,bool> upgradesPurchased = new Dictionary<string, bool>();

protected Dictionary<string,int> upgradeCost = new Dictionary<string, int>();

/* UPGRADE TYPES
* Title
* Layout
* Scoreboard
* VisualCountdown
*/
protected Dictionary<string,string> upgradeTypes = new Dictionary<string, string>();

protected bool IsPurchased(string upgradeName){
return upgradesPurchased [upgradeName];
}

public static string[] KeyByValue(Dictionary<string, string> dict, string val)
{
string key = null;
foreach (KeyValuePair<string, string> pair in dict)
{
if (pair.Value == val)
{
key += pair.Key + "|";
break;
}
}
return key.Split('|');
}

protected void ScanForAvailableUpgrades(){
AvailableUpgrades.Clear (); //Clear the list
int scanRange = GetUnits() + GameManager.Instance.scanRange; //Get the range by adding the scanRange to current units
for (int i = 0; i < upgradesOwned.Count; i++) { //Loop through all upgrades
if (IsPurchased (upgradeNames [i])) {
//return
} else {
if (scanRange >= upgradeCost [upgradeNames [i]]) { //Check if the cost is within the scanrange
if (AvailableUpgrades.Contains (upgradeNames [i])) { //Check if upgrade is already in the available list
//return; //If it is we just return
} else { //Else
if (upgradesOwned [upgradeNames [i]]) { //Check if the user already has the upgrade *NOTE* Should probaby check for this first to improve performance
//return; //If it is return
} else { //Else
AvailableUpgrades.Add (upgradeNames [i]); //Upgrade does not exist and is not bought so add it to the available list
}
}
}
}
}
}

进一步调试后,我发现 Debug.Log(keys[i]); 将第一个字符串返回为“basicTitle|”并且字符串字符串为空

编辑:我可以看到我的问题在哪里似乎具有误导性第 125 行是这一行 if (IsPurchased (upgradeNames [i])) { 但是我已经经历过并且在注释掉“DisableSameType”之后错误消失了方法DisableSameType 是一个新函数,错误直到它被实现后才开始发生,所以我 100% 肯定这是错误的来源我只是不确定是什么导致了它

最佳答案

upgradesOwned 字典中没有有效的key
在将 key value 设置为 false 之前,您应该检查 dictionary 是否有 key。如果它有那么你可以把它设为false,否则你需要在字典中添加一个键值对
所以,而不是你的代码中的这一行:

upgradesOwned [keys [i]] = false;   //Set them to false

应该是这样的:

if(upgradesOwned.ContainsKey(keys[i])){
upgradesOwned [keys [i]] = false; //Set them to false
}else{
upgradesOwned.Add(keys [i],false); //Add new key and set them to false
}

关于c# - key 不存在错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49849540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com