2020.코딩일지

[solidity] struct구조체 (feat.솔리디티깨부수기) 본문

Block Chain

[solidity] struct구조체 (feat.솔리디티깨부수기)

개발하는라푼젤 2022. 11. 30. 00:02
728x90

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract lec16 {
    struct Character { //나만의 타입 구조체 struct
        uint256 age;
        string name;
        string job;
    }

    mapping(uint256 => Character) public CharacterMapping;
    Character[] public CharacterArray;
    
//필수파라미터 입력 / returns에 
//..왜가스비가 안드는거지?
    function createCharacter(uint256 _age, string memory _name, string memory _job) pure public returns(Character memory) {
        return Character(_age, _name, _job);
    }
    function createCharacterMapping(uint256 _key, uint256 _age, string memory _name, string memory _job) public {
        CharacterMapping[_key] = Character(_age, _name, _job);
    }
    function getCreagteMapping(uint256 _key) public view returns(Character memory) {
        return CharacterMapping[_key];
    }
    //Array는 인덱스가 자동생성되니까 키값이 없는것.
    function createCharacterArray(uint256 _age, string memory _name, string memory _job) public {
        CharacterArray.push(Character(_age,_name,_job)); //구조체를 push
    }
    function getCharacterArray(uint256 _index) public view returns(Character memory) {
        return CharacterArray[_index];
    }
}
Comments