DEV Community

Loading Blocks
Loading Blocks

Posted on

Solidity Mappings type

Preface

Mappings store key-value pairs in Solidity, which similar to hash tables or dictionaries.

  1. definition & initialization

mapping(address => uint) public balances

Key types cannot be mappings or arrays.
Values can be any type, including nested mappings.

  1. writing and reading operation

writing/updating: [mappingName][key] = value;
reading: [mappingName][key]

Notes: reading a non-existent key returns the default value.

  1. limitation

mappings cannot be iterated
Auxiliary arrays can track keys

  1. nested mappings

mapping(address => mapping(uint => bool)) public permissions;

Nested mappings require multiple keys for access:

permissions[msg.sender][1]

Top comments (0)