メインコンテンツまでスキップ

Struct

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract Todos {
// structのblue printの宣言
struct Todo {
string text;
bool completed;
}

// struct Todo型の配列todosインスタンスを作成
Todo[] public todos;

// 配列todosに新しいstruct Todo型インスタンスtodoを要素として追加する関数
function addTodo(string calldata _text) public {
// structを初期化する3つの方法
// ①要素を引数として入れる関数のように作成
todos.push(Todo(_text, false));

// ②javascriptのobjectみたいに作成
todos.push(Todo({text: _text, completed: false}));

// ③空っぽなstructを作成して後から代入

Todo memory todo;
todo.text = _text;
// todo.completedの初期値はfalseだから代入は不要
// memoryに一時保存した値をstorageの値に書き出す
todos.push(todo);
}

// 'public'で宣言しているので、Solidityはgetter関数を用意してくれているのでこの関数はなくてもよい
function get(uint _index) public view returns (string memory text, bool completed) {
Todo memory todo = todos[_index];
return (todo.text, todo.completed); // 複数の値を返したいときによく使うパターン: (X, Y)
}

// textをアップデートする関数
function updateText(uint _index, string calldata _text) public {
Todo storage todo = todos[_index]; // ※storageは気にせず後の章で紹介する
todo.text = _text;
}

// todoが完了するとcompletedのbool値を切り替える
function toggleCompleted(uint _index) public {
Todo storage todo = todos[_index]; // storageにしないとmemoryになるので状態変数のtodoは書き換えられない
// storageにしないとmemoryになるのでtodoは書き換えられない
todo.completed = !todo.completed;
}
}

tip

Solidity を勉強する際には、Solidity を完全マスターするよりも、何か作りたいものを作れるようになることをゴールにしたほうが良いです。

Remixで試す