Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Enum :

/ iterate over enum cases


//enum weekDays : CaseIterable{
// case Monday
// case Tuesday
// case Wednesday
// case Thursday
// case Friday
//}
//// Print all weekdays
// for days in weekDays.allCases{
// print(days)
// }
//

// access enum raw values


// Type 1
//enum Size : Int{
// case small = 10
// case medium = 15
// case large = 20
//}
//var result = Size.small.rawValue
//print(result)

// Type 2 :
//enum Laptop {
// case brand(name : String)
// case price(Int)
//}
//var brand = Laptop.brand(name : "Macbook")
//print(brand) // brand(name: "Macbook")
//var price = Laptop.price(1000)
//print(price) //price(1000)

// Type 3 :
//enum Mercedes {
// case sedan(height : Double)
// case suv(height : Double)
// case roadster(height : Double)
//}
//var mycar = Mercedes.sedan(height : 34.6)
//switch (mycar){
//case let .sedan(height):
// print("Height :",height)
//case let .suv(height):
// print("Height",height)
//case let .roadster(height):
// print("Height",height)
//}
//// Output : Height : 34.6

//------------------------- Struct ---------------------------------


// How to initialize variable in struct
//struct Person{
// var name = " "
// var age = 0
// var studentID = 0
//}
//var info = Person()
//info.name = "AnTran"
//info.age = 14
//print("Hi my name is : \(info.name) ")
//print("and I am : \(info.age) years old")
//info.studentID = 123
//var anotherInfo = Person(name : "HuyDuc", age : 20)
//anotherInfo.studentID = 567
//print("another info has student ID : \(anotherInfo.studentID)")
//print("His name is : \(anotherInfo.name) and he is \(anotherInfo.age)")

// function inside Swift Struct


//struct Car {
// var gear = 0
// func applyBrake(){
// print("Applying Hydrauic Brake")
// }
//}
//var car1 = Car()
//car1.gear = 5
//print("Gear number : \(car1.gear)")
//car1.applyBrake()

//------------------------- Function -----------------------------------------------


// Omit argument labels
//func compare(_ a :Int , _ b : Int){
// if(a>b){
// print("\(a) is greater than \(b)")
// }
// else {
// print("\(b) is greater than \(a)")
// }
//}
//compare(3,4)

// func with variadic parameters


//func plus(numbers : Int...){
// var result = 0
// for num in numbers{
// result += num
// }
// print("\(result)")
//}
//plus(numbers:1,2,3,4,5)

// func with Inout parameters , when calling add & before variable name
//func changeName(name : inout String){
// if name == "Ross"{
// name = "Joey"
// }
//}
//var name = "Ross"
//changeName(name : &name)
//print(" After changing, the name is : \(name)")

//func return multiple values


func check(_ number : Int)->(Int,Int,Int){
var square = number * number
var cube = square * number
return(number,square,cube)
}
var result = check(5)
print("The square of 5 is : \(result.1)")
print("The cube of \(result.0) is : \(result.2)")

You might also like