根据外部读取的字符串检查字典的值 [英] Checking the values of a dictionary against a string being read in externally

查看:84
本文介绍了根据外部读取的字符串检查字典的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个这样的字典:

I've created a dictionary like so:

public Dictionary<string, string> myValues = new Dictionary<string, string>();

然后在我的开始方法中,我正在填写我的字典:

Then in my start method I'm filling out my dictionary like so:

void Start()
{
    myValues.Add("device one", "11111111111111");
}

我想要做的是检查我的价值在我的字典中创建,在上面的例子中,它将是远程读取的字符串值的 1111111111111 。我正在做的检查是这样的:

And what I'm trying to do is check the value that I've created in my dictionary, in the above example it would be the 1111111111111, against a string value that is being read in remotely. The check I'm doing is this:

void Update () 
{
    // Spawn a model based on the signal the ipad has recievied.
    if( myValues["device one"] == outputContent.text)
    {
        Instantiate(model1, new Vector3(-2.5f, 3.0f,0), Quaternion.identity);
    }       
}

该消息正在从一个包装类中解析出来来自本地ipad的信息,并将其直接传递给统一。获取消息的方法是这样的:

The message is getting parsed from a wrapper class that takes information from native ipad stuff and passes it direct to unity. The method for getting the message is this:

private void AppendString(string message) 
{
    outputContent.text += "\n" + message;
}

现在,事情是,传递的消息有效。当我运行我的代码时,屏幕会填满我想要的信息。但是当我尝试检查我已经存储的值与发送的值时,没有任何反应。

Now, the thing is, the message getting passed works. When I run my code, the screen gets filled with the information I want. But when I try to check the values I've stored against the ones getting sent in, nothing happens.

如何获得我的初始硬编码值是先从AppendString方法读入它们。我已经仔细检查了他们,以确保我有正确的信息。

How I got my initial hard coded value was by first reading them in from the AppendString method. And I've double checked them to make sure I've got the correct information down.

有人可以告诉我,如果我比较我的字典中持有的值,读取的值是正确的?

Can someone please tell me if I'm comparing the values held within my dictionary, to those being read in, is correct?

推荐答案

有几个可疑点。

这里添加 \\\
,你确定这是包含在字典的值吗?

Here, you add \n, are you sure that's included in the dictionary's value?

outputContent.text += "\n" + message;

另外,请确保正确比较字符串。甚至可能是一个大写字母的问题,这是一个让我痛苦的事情。例如:

Also, make sure you compare the strings properly. It may even be a capitalization problem, something that's bitten me many times. Try for instance:

if(String.Equals(myValues["device one"], outputContent.text, StringComparison.OrdinalIgnoreCase)

这篇关于根据外部读取的字符串检查字典的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆