【发布时间】:2014-02-24 14:15:46
【问题描述】:
我有以下字典,我正在填写通过第三方对象解析的数据。
在我的解析类中看起来像这样:
public string beaconMajor;
public string[] beaconValues;
public Dictionary<string, float> valueDictionary = new Dictionary<string, float>();
public string foo = "";
public void DistanceValue(string message)
{
foo = message.Split (',');
foreach (string b in foo)
{
beaconValues = beacon.Split('_');
beaconMajor = beaconValues[0];
string beaconDistance = beaconValues[1];
if(valueDictionary.ContainsKey(beaconMajor))
{
valueDictionary[beaconMajor] = float.Parse(beaconDistance);
}
else
{
valueDictionary.Add(beaconMajor, float.Parse(beaconDistance));
}
}
if (foo.Contains("-1"))
{
return;
}
}
然后在另一堂课中,我正在使用字典执行以下操作,只会向您显示一个条目,但有很多看起来像这样:
if (foo_class.valueDictionary.ContainsKey ("key"))
{
beacon1Scale = float.Parse (foo_class.valueDictionary["key"].ToString());
}
现在我想做的是按字典的值而不是键对字典进行排序。键是常量,但值每隔几秒就会改变一次。我试过以下代码:
List<KeyValuePair<string, float>> list = foo_class.valueDictionary.ToList();
list.Sort
(delegate(KeyValuePair<string, float> val1, KeyValuePair<string, float> val2)
{
return val1.Value.CompareTo(val2.Value);
}
);
Debug.Log(list);
dictionaryString = list.ToString() + "\n";
但是当我运行它时,我在 xCode 中收到以下消息:
System.Collections.Generic.List
1[System.Collections.Generic.KeyValuePair2[System.String,System.Single]] UnityEngine.Debug:Internal_Log(Int32, String, Object) UnityEngine.Debug:Log(Object) TriangulationScript:Update()
有人可以告诉我此消息的原因吗?
我仍在研究让这个字典通过值对自身进行排序,我什至尝试过使用 LINQ,但这与 xCode 不兼容,并且我在 Xcode 中出现 JIT 错误消息,如下所示:
ExecutionEngineException:正在尝试 JIT 编译方法 'System.Linq.OrderedEnumerable1:GetEnumerator ()' 使用 --aot-only 运行时。
如果有人知道任何其他方法可以做到这一点,请告诉我。我现在已经花了几天时间。
更新
我使用了下面建议的答案,并在 xcode 中收到了这条消息:
ExecutionEngineException:正在尝试 JIT 编译方法 'System.Linq.OrderedEnumerable
1<System.Collections.Generic.KeyValuePair2>:GetEnumerator ()' 同时使用 --aot-only 运行。在 System.Collections.Generic.List
1[System.Collections.Generic.KeyValuePair2[System.String,System.Single]].AddEnumerable (IEnumerable1 enumerable) [0x00000] in <filename unknown>:0 at System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.String,System.Single]]..ctor (IEnumerable1 集合)[0x00000] in :0 at System.Linq.Enumerable.ToList[KeyValuePair2] (IEnumerable1 来源) [0x00000] in :0 at TriangulationScript.Update () [0x00000] 在 :0
【问题讨论】:
-
回滚编辑,因为上述代码需要在 xCode 中编译,这就是我不能使用 LINQ 的原因。如果有人知道这样做的方法,请告诉我。
-
OT:你如何在 xcode 中使用 c#?
-
xCode 插件与 Unity。我所有的 C# 都是在 Unity 方面完成的。
标签: c# xcode linq dictionary