2

As far as i know solidity doesn't support passing an array of strings into a function ?

My issue is that i just added an array of strings (hashs from ipfs) into my struct and i am used to use the constructor to either create or update my data like this User storage x = User(_id,_someting); and now i can not pass the array into User. any simple way to go over this or how can kepp the files outside the struct but somehow link it with the id to the struct?

struct User{
unit256 id,
unit256 someting,
string [] files,

}

1 Answer 1

1

The struct merely defines a type. You'll need to lay out storage to hold the users. These patterns might give you some ideas: Are there well-solved and simple storage patterns for Solidity?

You can't pass an array of strings because strings are variable length and the array itself would be variable length - a variable length array of variable length elements. This is not (yet) supported by the interface.

In any case, you can work it out with functions designed to deal with one instance at a time.

For example (simplified) ...

struct UserStruct {
  uint something;
  string[] fileHashes;
  boot isUser;
}

mapping(address => UserStructs) public userStructs;

function isUser(address user) public view returns(bool isIndeed) {
  return(userStructs[user].isUser;
}    

function newUser(address user, uint something) public onlyOwner returns(bool success) {
  require(!isUser(user));
  userStructs[user].something = something;
  userStructs[user].isUser = true;
  return true;
}

function appendUserFile(address user, string fileHash) public onlyOwner returns(bool success) {
  require(isUser(user));
  userStructs[user].files.push(fileHash);
  return true;
}

... where user exists, add the new file hash to the array.

This is set up so we don't expect to populate everything in one move. Instead, we rely on the client to make multiple small transactions that address parts of the larger process.

Hope it helps.

2
  • How to get create a function that gets the length of fileHashes array by (address user) ?
    – FUN_
    Commented Sep 10, 2018 at 20:08
  • function getFileHashCountByUser(address user) public view return(uint count) { require(isUser[user]);return userStructs[user].fileHashes.length; } Commented Sep 10, 2018 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.