Access Mapping Variables¶
Mappings are accessed like arrays, but there are no index-out-of-bounds-exceptions. If you don't know what that means, don't worry, it was a joke for Java Developers.
All possible key/value pairs are already initialized. You can simply access any key and get back "false", since that's the default boolean value.
Give it a try?
Enter "0" next to "myMapping" and hit the button. It will return false.
The same happens with "1", "2", or "123123123123123". Any index will return false, because we didn't write a value there yet.
Write to a Mapping¶
If you have a look at our simple function setValue(uint _index)
, then you will see you will write "true" to an _index. So, if the _index = 0, then myMapping[0] will be set to true. Again, no need to initialize anything here, it will just assign the value.
Give it a try!
- Set the value of myMapping[0] to true, by entering "0" next to the "setValue" button
- Hit the setValue button
- retrieve the myMapping[0] value again, by entering "0" next to the "myMapping" button
- It should return true ✨