I`ve got the world on a String!

Lorenzo Piombini
4 min readOct 2, 2020

About String data type in a Calculator app

I wanted to quote Sinatra because He was singing that back in the 30s, as He knew already how powerful String data type is.

I am studying Swift language, and I find this data type really interesting, I am assuming that they are strong in other languages too, but since Swift is my interest for now, I will talk about it in iOS environment.

First off:

you can see Strings everywhere in the real world. Think about your email address, it is a string isn't it ? Even this little article is basically a “collections” of strings. your physical address is a String : “ 105 New England ave “ is a string even if you see those numbers there, everything between the double quote sign, it will be a string in Swift.

in The bootcamp that I am attending there was an assignment: Build a Calculator App, (here the link to my gitHub profile ) I used STRINGS TO MAKE IT WORKS! and you are probably thinking WHAT ? the only issue is that you will have to convert the string in number and vice versa in order to have the Calc functioning and showing the correct results and the wanted inputs, and a lot of logic has to be made in order to have the App working.

Strings look like a collection data type to me, particularly the Arrays. String and Array are really close siblings in my opinion, why ? Well, an Array is a way to store and organize data usually of the same type, e.i. Integer or strings; when I think about strings, I personally see an array of characters, if you take the following string: “Hello World” you can probably write the same in this way:

arrayOfString[0] =”H”

arrayOfString[1] = ”e”

arrayOfString[2] =”l”

arrayOfString[3] =”l”

…..

and so forth….a String is a collection of characters! you can create a string in these ways in Swift :

var result = “” this it'll be an empty string

var userInput:String = “” // you are declaring the data type(String in this case) of your variable

var greet = “Good Morning” this is the literal way to create one string

var history: [String] = [] // this is how you can create an Array of strings, in this case the collection data is empty.

the cool thing here is that you can access to the characters of your string, and make some tests. In the calc app, for example, you have to avoid the Overflow issue regarding integer data type, if the overflow occur, your App will crash and it is not a pleasant User experience. A number like 235876556543209 could be express also “235876556543209” as a string, now you can check its length with a method (or function) called .count and it will give you the number of characters as a result: 15 in my example, with this data you can set an IF statement where you can avoid the program to get into the overflow. another useful method is .contains(“”) I used it to display decimal numbers correctly in my Calculator App, like so:

if andthisLbl.text!.contains(“0.”)

forget about everything and focus on .contains(“0.”) , it will check your string wether it contains “0.” and the code after this statement will do some actions if the string contains the characters “0.”; You can avoid a possible mistake, for example, the user will press the point button to put the point and insert a number with decimals, what happen if you already have a value like 0.78 or 9.789 ? if you don`t handle it, you will have a ‘number’ with two points which is not right, with the method written above you can easily check it and avoid the double point. another useful method is: .append(“some char”) and .poplast(), the first will add whatever char you want to add at your string, the second one will erase the last char from you string. I use it in my app to add numbers, think at the moment you have the calc keyboard in front you and you hit the 1 button and the 2 button right after the one, your calc will display 12, this is possible because there is some code underneath those button that with a IF statement concatenation will predict every scenario and handle the inputs in the right way, among this code my idea was to use the method .append(“”) after some conditions to display the nmber correctly like :

if andthisLbl.text != “0” && operationclicked == true && handleInput == false{

andthisLbl.text?.append(“\(value)”)

}

if it seems strange what I am saying above is basically that IF the label is showing a number different than zero, (not equal is !=) AND ( && ) operationclicked is true AND (&&) handleInput is false then append a string in the variable value at the label which is showing the number in your calc.

operationcliked is a Boolean value that is trigged when you hit the operators button such has + - \ and X(for multiplication ) and handleInput it is a value created to handle this particular event in my app. what about .poplast() ??

I used it in my code to make the numbers nice, What ?? basically what is going to happen when you transform an Integer or a Double ( a really precise decimal number ) into a string, you might have some unwanted chars like in my app, for the percentage calc even though the result was an Integer and not a decimal number was displayed like so 50.0, I DO NOT LIKE IT . so here what i did :

if resultAndEnteringLbl.text!.contains(“.0”) {

resultAndEnteringLbl.text!.popLast()

resultAndEnteringLbl.text!.popLast()

}

this is really easy : IF your label display a number with the value .0 like 50.0 then delete the last char resulting in 50. which is still not right so ii used popLast again in order to delete also the “.” and the label will show the integer 50.

there is an Universe to talk about Strings and all the operations you can do with them, I will talk about that in another article

thank you for reading I hope it will be helpful for someone.

--

--