Skip to content

Instantly share code, notes, and snippets.

@Gabbrolee
Last active August 5, 2021 17:15
Show Gist options
  • Save Gabbrolee/d6a5cdf69cd36252bde8f1af5767a8fe to your computer and use it in GitHub Desktop.
Save Gabbrolee/d6a5cdf69cd36252bde8f1af5767a8fe to your computer and use it in GitHub Desktop.
DAY ONE : VARIABLE AND STRINGS, DATA TYPES, OPERATION, STRING INTERPOLATION, ARRAY
import UIKit
//MARKS:- VARIABLES AND CONSTANTS
/* variables are data stored that can have their value change whenever we want it and constant are value that can not be changed .. advaantage of using var in declaring a variable and let in declaring a constant is that it help xcode to remind us of the constant we have set and whenever we try to change it, it will not xcode will not compile our code and it help to save memory space that is optimization of memory. */
// create a name variable
var name = " James Bond"
// name can be updated without xcode throwing error message
//there is no need to redeclare it anymore
name = "Ade Osun"
// print what is in the name variable
print("\(name)") // Ade Osun is printed instead of James Bond and it is
//as a result of the variable name been updated
// let's create variable with let and try to change it
let address = " 17, James Bond Street"
// address has been alter and xcode will not run this code
// address = "8, Ade Osun Street"
//MARKS:- TYPES OF DATA
// 1. String : they are combination of characters
//it could be of million characters as the case maybe
// string declaration
var country = "Nigeria" //first method is type inference: is when //swift assing a datatype to variable or //constant base on the type of value assign //to the variable
var nation: String //second method is type annotation method
nation = "Nigeria"
//2. Integer: they are positive whole numbers
var age: Int
age = 25
//MARKS:- type safety is observe in swift and it simply means swift will not allow one to put values in a variable declared as Int into string variable
//age = "James Bond" /* swift will throw up error since age has been declared as an integer initially */
//3. FLOAT AND DOUBLE: these are use for decimal numbers and double is prefer to float because it holds more accuracy than float that is it can take more fractional digit than float
var latitude : Double
latitude = 36.1666677888 //36.1666677888 more accuracy
var longitude : Float
longitude = 36.1666677888 // 36.16667 less accuracy
//4. BOOLEAN: this is a data type that helps to set value to true, false or absolute
// Declaring variable as a boolean
var stayOutTooLate: Bool
stayOutTooLate = true
// it can also be declare this way
var comeHomeTooLate: Bool = true
//MARKS:- OPERATORS: These are the little symbols we learnt in our arithmetics
var a = 10
a = a + 1 /* top up the value of a with 10 and it will make it 11
a = a - 1 reduce the current value of a by 1 and it will make it 10
a = a * a multiply the current value of a by itself and it will make it 100 */
var b = 10
b += 10 // b is now 20
b -= 10 // b is now 10
var c = 1.1
var d = 2.2
var e = c + d // this will add the decimals together and gives 3.3
var name1 = "Tim McGraw"
var name2 = "Romeo"
var both = name1 + " and " + name2 // Tim McGraw and Romeo
var m = 9
var f = 2
var result = m % f // this returns the remainder and it is 1
// comparison operators are: < , > , <= , >= , == , !=
// they help to compare lefthand side operand to the righthand side
c > d // is c greater than d if yes then true
c >= e // is c greater than or equal to if yes then true
// you can negate a boolean with ! and it changes it to false if it is true
//MARKS:- STRING INTERPOLATION
// This is a way of combining variable and constant inside a string
var namee = "Tim McGraw"
var age1 = 25
var latitude1 = 36.166667
"Your name is \(namee), your age is \(age), and your latitude is \(latitude)."
// mathematical operation can be perform in string interpolation as well
"Your age is \(age1) and in 4 years, you will be \(age + 4)"
// output will be Your age is 25 and in 4 years, you will be 29years
//MARKS:- ARRAY
// swift use type inference to know what type of data your array holds
//it allows you group of data together into collections
//and allow you access those values in the array using the index.
var evenNumbers = [ 2, 4, 6, 8]
var songs = [ "Shake it off", "You belong with me", "Back to December" ]
// accessing the position of items in the array, you need to use the array name
//then the position . the first element is at index 0 and so on
songs[0] // Shake it off
songs[1] // You belong with me
// if you need to confirm the type of data the array holds
type(of: songs) // this is seeing songs as an array of strings
// if you want an array to take any type then you have to declare it as array of Any
var street:[Any] = [ "4, Ohemene Street", "14, kemi Adeosun", "17, Ayoola Street"]
// empty array declaration
var song: [String] = [] // empty array
song[0] = "Shake it Off" // assign the string Shake it Off to index 0f
// the array
var songs1 = [ "Shake it off", "You belong with me", "Back to December" ]
var songs2 = [ "let's go home", "Where have you been", "It's public holiday" ]
var both1 = songs1 + songs2
// or
songs1 += songs2
// You can append strings to an array
@wptechprodigy
Copy link

Great work.

Try to cut a long sentence into several lines for ease of reading.
Consider line 5, it's too long to fit in on a line,
you could break it into several lines for easy reading.

If you look at my two immediate statements above, I could have put then on a line
however, I broke them into two lines so it's easy to read.

One more thing, take note of the title naming: Not DAY ONE but DayOne and other parts are fine.
Also, the file name should be DayOne.txt or DayOne.swift and not gistfile1.txt

Well done and I'll be expecting DayTwo today!

@wptechprodigy
Copy link

Check out this sample gist I created for your reference:

https://gist.github.com/wptechprodigy/94aee9c38af27dc87f79cbb2607aeb5f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment