Member-only story
Solidity for Swift Developers: File Structure and Functions
Get started with Solidity in Swift

Solidity is an object-oriented language to write smart contracts that can be deployed on the blockchain, for instance, Ethereum. The syntax is similar to Javascript, but on the other hand, semantics is closer to C++. We will dig into Solidity language structure and functions from a Swift developer’s perspective.
Pragma and sfile extension
Before we jump into the Solidity language structure, we need to know what a pragma
keyword is and what it means.
Unlike the Objective-C #pragma
and Swift MARK
, the' pragma' keyword in Solidity describes what version of the compiler should use. Keep in mind that it instructs the compiler to check if the versions do match. It does not turn on or off any language features.
For instance, if we would like to tell the Solidity compiler that we want to use the 0.8.x version, we can start the source file like this:
pragma solidity ^0.8.0;
We can also use greater or smaller operations to describe even the version interval, which could be handy.
There are other ways to use the pragma
keyword, but we won't look into that this time.
Solidity source files are saved with the .sol
extension.

Everything starts with a Contract
Contracts in Solidity language are similar to classes in Swift. Contracts contain state variables, functions, function modifiers, events, errors, structs, and enums. This time we are only going to look into function structure.
Like classes, we need to name it and open, and close with braces.
contract HelloSwiftFromSolidity {}
State Variables
State variables are variables that are declared inside the contract
. Be aware that this information is stored on the blockchain contract storage once you deploy it. We don't need to worry about…