Last active
July 17, 2022 22:41
-
-
Save josephajibodu/d30cfb8f212d0ce335dfcc1481da9fc6 to your computer and use it in GitHub Desktop.
Basic Overview of the Swift Programming Language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1. Constants and Variables | |
// Variables - value of a variable can be changed at any point in the cod | |
var name = "Joseph" | |
name = "Joseph O." // ☑️ | |
//Constant - value of a constant can't be changed once it's set | |
let lastname = "Ajibodu" | |
//lastname = "Another Name" // (This will give an error) | |
// Multiple constants/variables on the same line | |
let x = 0.0, y = 0.0, z = 0.0 | |
//2. Type Annotation | |
//Syntax: var <variableName>: Type | |
var gender: String | |
var title: String = "Mr" | |
// NB: Multiple related variables of same type can be defined on the same line, with a single type annotation after the variable name. | |
var green, red, blue: Double | |
// 3. Naming Constants & Variables | |
/* | |
- can contain unicode characters | |
- no whitespace, mathematical symbols, arrows, line & box drawing characters, private-use unicode scalar vectors | |
- can't begin with number | |
*/ | |
// 4. Printing constants and variables | |
// Syntax: print(_:separator:terminator:) | |
// Only the first argument is required, separator and terminator have default values | |
print("Hello World!") | |
print(name) | |
// 5. String Interpolation | |
// Swift uses string interpolation to include the name of a constant or variable as a placeholder in a longer string, and to prompt Swift to replace it with the current value of that constant or variable. | |
// Wrap the variable name in parentheses() and escape it with a backslash\ before the opening parenthesis: \(variableName) | |
var age = 34 | |
print("The current value is \(age)") // Will output: The current value is 34 | |
// 6. Comments | |
//Single Line comment | |
/* Multi | |
Line | |
Comment | |
*/ | |
// Multiline comments can be nested | |
/* | |
Outer comment | |
/* Multi | |
Line | |
Comment | |
*/ | |
*/ | |
// 7. Semicolons | |
//Its not required in Swift, but you can use it if you prefer to have it. | |
//However, its required when you have multiple statements on the same line | |
let 😇 = "Happy!";print(😇); | |
// 8. Data Types | |
// - Integers (Int, UInt, Int<base>, UInt<base> ...) where base = 8, 16, 32, 64 | |
// Int8, Int16, Int32, Int64 - are signed integers of 8, 16, 32, 64 bits respectively. | |
// Int should be used where no specific range is required | |
// Same applies to UInt - only that it's unsigned integer, i.e. integers with no negative value. | |
// More on the following later. | |
let val: UInt8 = 4 | |
//When there's no specific requirement for type of Integer, it's recommenede you use Int | |
let maxValue = UInt8.max; print(maxValue); | |
let minValue = UInt8.min; print(minValue); | |
// - Floating Point Numbers | |
//Floating-point numbers are numbers with a fractional component | |
// We have Double(64bit) & Float(32bit) | |
// 9. Type Safety and Type Inference | |
let randomValue = 43; | |
// Swift infers the type to be Int | |
var pi = 3.142 | |
// Swift infers the type to be Double | |
let anotherPi = 3 + 0.14159 | |
// Swift infers the type to be Double | |
// 10. Numeric Literal | |
// You can pad numbers or add underscores to help with readability | |
let userId = 000123 | |
let justOverOneMillion = 1_000_000.000 | |
let exponentDouble = 1.21875e3 // 1.21875 x 10^3 | |
// 11. Numeric Type Conversion | |
// Conversion should be done to Feasible range | |
// e.g. you can't convert -45 to UInt because the UInt.min is 0 (-45 is not in the range of UInt) | |
let num1 = 4.23 | |
let num2 = Int(num1) | |
let num3 = UInt(num1) | |
print(num2) | |
let three = 3 | |
let pointOneFourOneFiveNine = 0.14159 | |
let pi1 = Double(three) + pointOneFourOneFiveNine | |
print(pi1) | |
// 12. Type Alias | |
// Type aliases define an alternative name for an existing type. You define type aliases with the typealias keyword. | |
typealias AudioSample = UInt16 | |
// Now AudioSample is an alias for UInt16 | |
var maxAmplitudeFound = AudioSample.min | |
// 13. Boolean (true or false) | |
let isValue = true //Inferred as Bool | |
let isAbsent: Bool = false | |
// 14. Tuples | |
// Tuples group multiple values into a single compound value. | |
// Values in a tuple can be of any type | |
// Can be represented with parenthesis () separated by comma | |
let score1 = ("joseph", 89) | |
// scoree1 is of type (String, Int) | |
print(score1) | |
/* Tuples can contain multiple element, you can take | |
advantage of tuple to return multiple values from | |
a function | |
*/ | |
// 15. Optional | |
//You use optional where a value may be absent | |
//Optional value can be denoted with a question mark after the type | |
var middleName: String? | |
//You set an optional variable to a valueless state by assigning it the special value nil: | |
var serverResponseCode: Int? = nil | |
//Assigning nil to a "non optional" value will give an error | |
let pi3: Int | |
// pi3 = nil // Error: 'nil' cannot be assigned to type 'Int' | |
//Some expressions return Optional Type : See below | |
let possibleNumber = "123" | |
let convertedNumber = Int(possibleNumber) | |
// convertedNumber is inferred to be of type "Int?", or "optional Int" | |
// Why? This is because the conversion might fail | |
// Consider the case below | |
var status = "M" | |
var statusCode = Int(status) | |
print(statusCode) // prints 'nil' | |
//This means Int() can return either a numeric integer value or nil, which is a case we refer to as optional Int |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment