【问题标题】:Invalid value type: String for Integer无效的值类型:整数的字符串
【发布时间】:2012-09-06 13:38:41
【问题描述】:

以下代码

public List<Map<String,String>> getPhoneData() {
    List<Map<String, String>> mapp = new List<Map<String, String>>();
    Map<String, Integer> temp
         = new Map<String, Integer>{'type' => 'Profile Value', 'count' => 1};
    return mapp;
}

给我

Compile Error: Invalid value type: String for Integer

在第 3 行

我想用一个值String 和另一个Integer 创建Map。该怎么做?

【问题讨论】:

  • 错误说明了问题所在,您的 Map 是 并且您正在传递它 {String => String}。改为传递它 {String => Integer}。
  • @EricLeschinski:重点是typeString。现在检查我的问题
  • 不要在别人回答后改变问题的性质。

标签: salesforce apex-code


【解决方案1】:

删除数字周围的引号,如下所示:

Map<String, Integer> temp = new Map<String, Integer>{'type' => 123, 'count' => 1};

编辑: 当您完全改变了您的问题时,实现这一目标的最佳方法是:

public List<Map<String,String>> getPhoneData() {
    List<Map<String, String>> mapp = new List<Map<String, String>>();
    Map<String, String> temp = new Map<String, String>{'type' => 'Profile Value', 'count' => '1'};
    return mapp;
}

【讨论】:

  • 我不只是改变了 type 的值来显示它实际上是一个字符串
  • 您将地图从&lt;String, Integer&gt; 更改为&lt;String, String&gt;'type' =&gt; '123' 更改为'type' =&gt; 'profile value'。这改变了问题和解决方案。查看我更新的答案以获取解决方案。
  • 我提出这个问题的重点是因为typeStringcountInteger。如果我要设置这种类型的值。我该怎么办?
  • 不是。不可能。您不能同时拥有一个包含字符串和整数作为键/值的 Map。
  • 是的,这正是问题所在。您只能将映射定义为 。不像 。您可以根据需要将地图指定为 ,但这很丑。
猜你喜欢
  • 2014-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多