0% found this document useful (0 votes)
10 views

ChapterStructureVC

The document contains Swift code defining a UITableViewCell subclass, a UIViewController subclass, and several data structures including Points and Student. It demonstrates the use of mutating methods, class and structure instances, and the differences between reference and value types in Swift. Additionally, it includes methods for managing table view data and handling button actions in a user interface.

Uploaded by

zvipin.ahlawat
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

ChapterStructureVC

The document contains Swift code defining a UITableViewCell subclass, a UIViewController subclass, and several data structures including Points and Student. It demonstrates the use of mutating methods, class and structure instances, and the differences between reference and value types in Swift. Additionally, it includes methods for managing table view data and handling button actions in a user interface.

Uploaded by

zvipin.ahlawat
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import UIKit

class CheckBoxCell: UITableViewCell {

@IBOutlet weak var lblSchoolName: UILabel!


@IBOutlet weak var btnCheckBox: UIButton!

@IBAction func checkBox(_ sender: Any) {

btnCheckBox.setTitleColor(.red, for: .normal)


}
}

struct Points{
var x = 0.0, y = 0.0
mutating func moveBy(x deltaX: Double){
print("before = ",self)
//automatic create new instance after it replace existing one with self
x = x + deltaX
print("after = ",self.x)
}
mutating func stepBy(x deltaX: Double){
print("before = ",self.x)
//Assigning to self Within a Mutating Method
self = Points(x: self.x + deltaX, y: self.y)
print("after = ",self.x)
}
static func forwardBy(x deltaX: Double){
print(self)
}
}

class School: UIViewController {


var student = Student() //struture object
var schoolRegistered = false
var schoolRank = 0.0
var schoolName : String = ""

override func viewDidLoad() {


super.viewDidLoad()

}
}

struct Student {
var studentId = 0
var studentRollNo = 0
}

enum player{
case play, pause, start, stop
mutating func turnPause() {
self = .pause
}
}

let student = Student(studentId: 20, studentRollNo: 30) //Memberwise Initializers


for Structure Types

class ChapterStructureVC:
UIViewController,UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) ->
Int {
return 10
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->


UITableViewCell {

let cell: CheckBoxCell = self.tableView.dequeueReusableCell(withIdentifier:


"cell", for: indexPath) as! CheckBoxCell
cell.lblSchoolName.text = "Lorem Ipsum is simply dummy text of the printing
and typesetting industry. Lorem Ipsum has been the industry's standard dummy text
ever since the 1500s, when an unknown printer took a galley of type and scrambled
it to make a type specimen book. It has survived not only five centuries"
// let selected = [UIImage ]
// cell.btnCheckBox.setImage(, for: UIControlState)

return cell
}

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {


super.viewDidLoad()

func structAndClassInstance(){

let someStudent = Student()


let someSchool = School()

print("someStudent english \(someStudent.studentId)")


print("someSchool math \(someSchool.student.studentId)")

print("hd resolution \(student.studentId)")

let newStudent = student //structure copied the value


print("cinema width \(newStudent.studentId)") //this has new value
print("hd width \(newStudent.studentId)") //this has old value
}

func abccheck() -> String{


return "Helo world"
}

func callingPrintStruct(){
self.abccheck()

/*var structObj = Student()


print("Default value \(structObj.studentId) and height \
(structObj.studentRollNo)")
structObj = Student(studentId: 20, studentRollNo: 30)
print("New value \(structObj.studentId) and \(structObj.studentRollNo)")*/

var somePoint = Points(x: 1.0, y: 1.0)


somePoint.moveBy(x: 3)
print("somePoint x = \(somePoint.x) and somePoint y = \(somePoint.y)")
Points.forwardBy(x: 67)
}

func mutatingFunc(){

var currentState = player.start


let rememberedState = currentState
currentState.turnPause()
print("The current State is \(currentState)")
print("The remembered state is \(rememberedState)")
}

///////////////Class
func classReference(){

let tenEighty = School() //class object create


tenEighty.student = student //class have structure type object so hd is
also structure
tenEighty.schoolRegistered = true
tenEighty.schoolName = "1080i"
tenEighty.schoolRank = 25.0

//classes are reference types, tenEighty and alsoTenEighty actually both


refer to the same VideoMode instance.
let alsoTenEighty = tenEighty
print("The frameRate property often Eighty is now \(tenEighty.schoolRank)")

//compare two classes object from ===


if tenEighty === alsoTenEighty{
print("tenEighty and alsoTenEighty refer to the same VideoMode
instance.")
}

You might also like