Created
December 28, 2020 11:27
-
-
Save devreena03/f1cdad38aa8dec7335aac90f46e39dfb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import UIKit | |
//var and const | |
var str = "Hello, playground, since" | |
str = "some"; | |
let cons = "hello" | |
//datatype | |
var a:Int = 2 | |
var b = 5 | |
print(a + b) | |
print(a-b) | |
print(b/a) | |
print(b%a) | |
//String | |
let apples = 3 | |
let oranges = 5 | |
let appleSummary = "I have \(apples) apples." | |
let fruitSummary = "I have \(apples + oranges) pieces of fruit." | |
//Array | |
var arr1 = [String]() //initiaising an array | |
var arr = ["dog","cat","bird"]; | |
arr.count | |
arr[0] | |
//iteration | |
for index in 0...2 { | |
print(arr[index]) | |
} | |
for item in arr { | |
print(item) | |
} | |
//add element to existing array | |
arr += ["mouse"] //at end | |
arr | |
arr[2] = "toto" // at a index | |
for item in arr { | |
print(item) | |
} | |
//remove at a index | |
arr.remove(at: 3) | |
arr | |
//Dictionary | |
var dic = Dictionary<String, String>() //type of key:type of value | |
var d2 = [Int:String]() //type of key:type of value | |
var occupations = [ | |
"Malcolm": "Captain", | |
"Kaylee": "Mechanic", | |
] | |
occupations["Jayne"] = "Public Relations" | |
occupations | |
//adding key value pair | |
dic["first"] = "first value" | |
dic["sec"] = "2nd value" | |
print(dic) | |
//retriving data | |
print(dic["sec"]) | |
//update a value for a key | |
dic["sec"] = "updated value" | |
print(dic) | |
//iterating | |
for (key,value) in dic { | |
print("\(key) has value: \(value)") | |
} | |
//remove | |
dic["sec"] = nil | |
print(dic) | |
//Tuples | |
var tuples = ("one", 1, 7.8) | |
print(tuples) | |
var status = (code: 500, desc:"internal server error"); | |
print(status.code) | |
//IF statement | |
let some=5 | |
if some<4{ | |
print("some is less than",a) | |
} else if some == 4 { | |
print("some==4") | |
} else { | |
print("some is greater than",a) | |
} | |
//Switch | |
var ch:Character = "c" | |
switch ch { | |
case "c": | |
print("c") | |
case "b": | |
print("b") | |
default: | |
print("some default") | |
} | |
//for | |
for _ in 1...5 { | |
print("hello") | |
} | |
for index in 1...5 { | |
print("hello",index) | |
} | |
let interestingNumbers = [ | |
"Prime": [2, 3, 5, 7, 11, 13], | |
"Fibonacci": [1, 1, 2, 3, 5, 8], | |
"Square": [1, 4, 9, 16, 25], | |
] | |
var largest = 0 | |
for (kind, numbers) in interestingNumbers { | |
for number in numbers { | |
if number > largest { | |
largest = number | |
} | |
} | |
} | |
largest | |
//while | |
var n = 2 | |
while n < 100 { | |
n *= 2 | |
} | |
print(n) | |
// Prints "128" | |
//repeat while | |
var m = 2 | |
repeat { | |
m *= 2 | |
} while m < 100 | |
print(m) | |
//function | |
func greet(person: String, day: String) -> String { | |
return "Hello \(person), today is \(day)." | |
} | |
greet(person: "Bob", day: "Tuesday") | |
//use of tuples | |
func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) { | |
var min = scores[0] | |
var max = scores[0] | |
var sum = 0 | |
for score in scores { | |
if score > max { | |
max = score | |
} else if score < min { | |
min = score | |
} | |
sum += score | |
} | |
return (min, max, sum) | |
} | |
let statistics = calculateStatistics(scores: [5, 3, 100, 3, 9]) | |
print(statistics.sum) | |
// Prints "120" | |
print(statistics.0) | |
// Prints "120" | |
//nested function | |
func returnFifteen() -> Int { | |
var y = 10 | |
func add() { | |
y += 5 | |
} | |
add() | |
return y | |
} | |
returnFifteen() | |
//function as return type | |
func makeIncrementer() -> ((Int) -> Int) { | |
func addOne(number: Int) -> Int { | |
return 1 + number | |
} | |
return addOne | |
} | |
var increment = makeIncrementer() | |
increment(7) | |
//function as an argument | |
func hasAnyMatches(list: [Int], condition: (Int) -> Bool) -> Bool { | |
for item in list { | |
if condition(item) { | |
return true | |
} | |
} | |
return false | |
} | |
func lessThanTen(number: Int) -> Bool { | |
return number < 10 | |
} | |
var numbers = [20, 19, 7, 12] | |
hasAnyMatches(list: numbers, condition: lessThanTen) | |
//Class | |
class Shape { | |
var numberOfSides = 0 | |
func simpleDescription() -> String { | |
return "A shape with \(numberOfSides) sides." | |
} | |
} | |
var shape = Shape() | |
shape.numberOfSides = 7 | |
var shapeDescription = shape.simpleDescription() | |
//constructor | |
class NamedShape { | |
var numberOfSides: Int = 0 | |
var name: String | |
init(name: String) { | |
self.name = name | |
} | |
func simpleDescription() -> String { | |
return "A \(name) with \(numberOfSides) sides." | |
} | |
} | |
var nshape = NamedShape(name: "Square"); | |
nshape.numberOfSides = 4; | |
nshape.simpleDescription(); | |
//inheritance | |
class Square: NamedShape { | |
var sideLength: Double | |
var perimeter: Double { | |
get { | |
return 3.0 * sideLength | |
} | |
set { | |
sideLength = newValue / 3.0 | |
} | |
} | |
init(sideLength: Double, name: String) { | |
self.sideLength = sideLength | |
super.init(name: name) | |
numberOfSides = 4 | |
} | |
func area() -> Double { | |
return sideLength * sideLength | |
} | |
override func simpleDescription() -> String { | |
return "A square with sides of length \(sideLength)." | |
} | |
} | |
let test = Square(sideLength: 5.2, name: "my test square") | |
test.area() | |
test.simpleDescription() | |
//error handling | |
//create error type | |
enum PrinterError: Error { | |
case outOfPaper | |
case noToner | |
case onFire | |
} | |
//throw error | |
func send(job: Int, toPrinter printerName: String) throws -> String { | |
if printerName == "Never Has Toner" { | |
throw PrinterError.noToner | |
} | |
return "Job sent" | |
} | |
//handle error | |
do { | |
let printerResponse = try send(job: 1040, toPrinter: "Never Has Toner") | |
print(printerResponse) | |
} catch { | |
print(error) | |
} | |
// Prints "Job sent" | |
let printerSuccess = try? send(job: 1884, toPrinter: "Mergenthaler") | |
let printerFailure = try? send(job: 1885, toPrinter: "Never Has Toner") | |
//optional | |
//let someValue:Int? = 5 | |
//var someAnotherValue:Int! | |
//print(someValue) | |
//print(someValue!) //unwrapping an optional value | |
//print(someAnotherValue) | |
//Optional handling with if else | |
var someValue:Int? | |
if someValue != nil { | |
print("It has some value \(someValue!)") //still need to unwrap to access the value | |
} else { | |
print("doesn't contain value") | |
} | |
//if let - optional binding | |
var someAnotherValue:Int? | |
if let temp = someAnotherValue { | |
print("It has some value \(temp)") // no need of unwrap | |
} else { | |
print("doesn't contain value") | |
} | |
//guard let | |
func testFunction() { | |
let someValue:Int? = 5 | |
guard let temp = someValue else { | |
return | |
} | |
print("It has some value \(temp)") | |
} | |
testFunction() | |
class Person { | |
var residence: Residence? | |
} | |
class Residence { | |
var numberOfRooms = 1 | |
} | |
let john = Person() | |
//let roomCount = john.residence!.numberOfRooms | |
// this triggers a runtime error | |
if let roomCount = john.residence?.numberOfRooms { | |
print("John's residence has \(roomCount) room(s).") | |
} else { | |
print("Unable to retrieve the number of rooms.") | |
} | |
//Protocol | |
//protocol SomeProtocol { | |
// init() | |
//} | |
class SomeSuperClass { | |
init() { | |
// initializer implementation goes here | |
} | |
} | |
protocol SomeProtocol { | |
init(someParameter: Int) | |
} | |
class SomeClass: SomeProtocol { | |
required init(someParameter: Int) { | |
// initializer implementation goes here | |
} | |
} | |
//class SomeSubClass: SomeSuperClass, SomeProtocol { | |
// // "required" from SomeProtocol conformance; "override" from SomeSuperClass | |
// required override init() { | |
// // initializer implementation goes here | |
// } | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment