1

为什么@assigned 总是0?

var gates:XML = <gates>
    <gate_id>
      10991
    </gate_id>
    <gate_id>
      10999
    </gate_id>
    <gate_id>
      11000
    </gate_id>
    <gate_id>
      11001
    </gate_id>
  </gates>;

var assignedGateIds:Array = ["10991","10999"];

for each(var xgate:XML in gates..gate_id){
    var gateId:String = xgate.text();

    if(assignedGateIds.indexOf(xgate.text())>-1){
        xgate.@assigned = 1;
    }else{
        xgate.@assigned = 0;
    }
}
4

2 回答 2

0

改变

    if(assignedGateIds.indexOf(xgate.text())>-1){

    if(assignedGateIds.indexOf(gateId)>-1){

或者

    if(assignedGateIds.indexOf(String(xgate.text()))>-1){

或者

    if(assignedGateIds.indexOf(xgate.text().toString())>-1){
于 2013-03-28T13:33:10.217 回答
0

很简单,因为 text() 函数返回的不是字符串而是对象。当您使用跟踪时,它会转换为字符串,但如果您检查一下,trace(getQualifiedSuperclassName(xgate.text()));您会看到它返回一个对象。

为了解决这个问题,我通常使用 trace(getQualifiedSuperclassName(xgate.text()+''));并将其强制为字符串,您也可以使用as String

var gates:XML = <gates>
    <gate_id>
      10991
    </gate_id>
    <gate_id>
      10999
    </gate_id>
    <gate_id>
      11000
    </gate_id>
    <gate_id>
      11001
    </gate_id>
  </gates>;

var assignedGateIds:Array = ["10991","10999"];

for each(var xgate:XML in gates..gate_id){
    var gateId:String = xgate.text();

    if(assignedGateIds.indexOf(xgate.text()+"")>-1){ // <- added +'' here
        xgate.@assigned = 1;
    }else{
        xgate.@assigned = 0;
    }
}
于 2013-03-27T19:04:15.820 回答