Skip to content

Latest commit

 

History

History
120 lines (72 loc) · 2.68 KB

File metadata and controls

120 lines (72 loc) · 2.68 KB

struct questions 1 ~ 3

1) What is struct? How to declare a structure?

Answer:

  • struct is a keyword used to create User Defined Datatype.
struct Car {

	let model: Int = 2023

	func start() {
		print("Starting")
  }

	func stop() {
		print("Stopping")
	}

}

let car: Car = Car()
car.start()
car.stop()

2) What is mutating keyword in Swift?

Answer:

  • mutating is a keyword which allows to modify structure variable inside the structure’s method
struct Car {

	let model: Int = 2023
	var speed: Int = 0

	func start() {
		print("Starting")
  }

	func stop() {
		print("Stopping")
	}

	// If you don't put `mutating` keyword inside a method which modifies
	// struct's properties, compiliation error will occur
	mutating func speedUp(newSpeed: Int) {
		speed = newSpeed
	}

}

let car: Car = Car()
print(car.speed) // 0
car.speedUp(newSpeed: 5)
print(car.speed) // 5

3) Is struct Value type or Reference Type?

  • Structure is a value type. Structure values are copied when you pass then to a method.

#Info

  • Value Type creates new instances and assigned when they passed to a method or assigned
  • Reference Type just shares the address of that Object when they are passed as arguments or assigned

Table Of Contents

Section 1, Data Type

Section 2, Operator

Section 3, Conditional Statement

Section 4, Enum

Section 5, functions

Section 6, struct

Section 7, initializers

Section 8, closures

Section 9, OOP

Section 10, static type vs dynamic type

Section 11, optional

Section 12, generic

Section 13, subscript

Section 14, access specifier

Section 15, higher order function

Section 16, delegate

Section 17, extension

Section 18, Memory Management

Section 19, protocols

Section 20, collections

Section 21, KVO and KVC

Section 22, Exception Handling

Section 23, Framework

Section 24, Objective-C