In Solidity, you can use the mapping keyword to map addresses to boolean values. A mapping is a data structure that associates keys with values. In Solidity, the mapping type allows you to define the types of the key and the value. In our case, the key is an address (address), and the value is a boolean (bool).
Here is a simple example of declaring and using such a mapping:
solidity// Specify compiler version pragma solidity ^0.8.0; contract AccessControl { // Create a mapping that maps addresses to boolean values mapping(address => bool) private accessList; // Function to set access permissions for an address function setAccess(address _addr, bool _hasAccess) public { accessList[_addr] = _hasAccess; } // Check if a specified address has access permission function checkAccess(address _addr) public view returns (bool) { return accessList[_addr]; } }
In this example, the AccessControl contract contains a mapping named accessList, where the key is of address type and the value is of bool type. This mapping is used to store the access permission status for different addresses.
- The
setAccessfunction accepts two parameters: an address and a boolean value. It sets the access permission for the specified address to the provided boolean value. - The
checkAccessfunction accepts an address as a parameter and returns whether the address has access permission (trueorfalse).
This type of mapping is very useful for creating access control systems, voting systems, state tracking, and other applications.
2024年7月21日 19:45 回复