Blockchain in Swift

Defining Block in Swift

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

In this video segment, you will learn how to define Blockchain Block class in Swift and declare its component as variables.

Keywords

  • Blockchain
  • Block
  • Hash in Block
  • Hash in Blockchain
  • PreviousHash in Block
  • Data in Blockchain
  • Block class
  • Block class in Swift
  • Swift Blockchain class

About this video

Author(s)
Elshad Karimov
First online
29 September 2019
DOI
https://doi.org/10.1007/978-1-4842-5382-3_4
Online ISBN
978-1-4842-5382-3
Publisher
Apress
Copyright information
© Elshad Karimov 2019

Video Transcript

Defining block in Swift. In this video, I will explain and develop the logic behind block class. Let’s switch to our application to continue. Before defining block, we need to break down what a block is. We define earlier that a block consists of three piece of data– the hash, the actual data to be recorded, and previous block’s hash.

Where we want to put our blockchain? We have to know whether the block is the first one or the second one. Well, we can easily define this in Swift. OK, let’s get started to add our properties to the block class.

First one will be hash, which is a string. The second one is data, which is also a string. The third one will be previous block’s hash, which is also string. And we will add index to identify the second block.

And now, an important in code needs to be added. As I mentioned earlier, how every time a block is modified, it has chains. This is one of the big features of block chains which makes it secure. So we have to create a function which can read a hash filled with random letters and numbers.

To create this function, we will need a few lines of code. Function generate, hash, access no parameter, and which will return string. And this is our function. We need to an object which gives us a universally unique value.

In Swift, we can do this by using NSUUID object, which represent the universally unique value. Return will be NSUUID to string. And this value returns with a dash. We needed to remove that from this string, replacing occurrence of.