29待办事项列表

// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;

contract TodoList{
    struct Todo{
        string text;
        bool completed;
    }

    Todo[] todos;

    function create(string calldata _text) external {
        todos.push(Todo(_text,false));
    }
    function updateText(uint _index, string calldata _text) external {
        // 两种方法
        // 方法1:如果只修改一个变量,这个方法消耗的gas更少
        todos[_index].text=_text;
        //方法2:如果修改多个变量,这个方法消耗的gas更少
        // Todo storage todo=todos[_index];
        // todo.text=_text;
    }
    function get(uint _index) external view returns (string memory,bool){
        //storage使用的gas比memory更少
        // storage此时只拷贝一次,而memory拷贝两次
        Todo storage todo=todos[_index];
        return (todo.text,todo.completed);
    }
    function toggleCompleted(uint _index) external {
        todos[_index].completed=!todos[_index].completed;
    }
}