¿Cuál es la longitud de una matriz de estructura?

Hice un contrato de billetera para almacenar todos los tokens que posee una cuenta.

Hay un mapping(address=>TokenInfo[]), y addressse refiere a la cuenta, TokenInfo[] tokens almacenados que contiene esta cuenta. TokenInfo es una matriz de la estructura TokenInfo, en la que tokenestá la dirección del token, existlo que significa si el token se ha agregado a la matriz.

contract Wallet {
    struct TokenInfo {
        IERC20Token token;
        uint exist;//0 means unregistered
    }

    mapping(address=>TokenInfo[]) public tokenList;

    function Wallet() {

    }

    function addToken(address _account, IERC20Token _token) public {
        for (uint i = 0; i < tokenList[_account].length; i++) {//seems didn't enter the loop
            if (tokenList[_account][i].exist == 0) {
                var tokenInfo = TokenInfo(_token, 1);
                tokenList[_account].push(tokenInfo);
            }
        }
    }

    function getTokenListLength(address _account) public constant returns (uint) {
        return tokenList[_account].length;
    }
}

Pero cuando llamo a addToken()la función, no funcionó. Parece que nunca entró en el bucle.

Respuestas (1)

El valor predeterminado en una asignación para una matriz es una matriz vacía.

En su caso, esto significa tokenList[_account].lengthque es igual a 0.

Y nunca se ingresará al bucle porque la condición i < tokenList[_account].lengthse convierte 0 < 0en falsa.