Skip to content

Latest commit

 

History

History

section12-generic

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Generics 1 ~ 4

1) What is Generic?

Answer:

  • Generic is a feature of Swift which helps to write Generalized / Reusable datatypes and methods.
  • Generic is a feature of Swift which helps to write generalized programming.
  • A Datatype which is capable of holding different types of values. Generic Datatype is not specific to Particular Datatype Values.

2) What is Generic Method? What is Generic Type?

Answer:

  • A method which is capable of performing operations on different datatype values.
/** Before Refacotoring, Stack can only contain 'Int' **/
class Stack {

	var collection: [Int] = []

	func push(item: Int) {
			collection.append(item)
	}

	func pop() {
			collection.removeLast()
  }

	func display() {
			print(collection)
	}

}

var intStack: Stack = Stack()
intStack.push(item: 10)
/** After Refactoring, Stack can hold multiple types **/
class Stack<T> {

	var collection: [T] = []

	func push(item: T) {
			collection.append(item)
	}

	func pop() {
			collection.removeLast()
  }

	func display() {
			print(collection)
	}

}

var intStack: Stack<Int> = Stack()
intStack.push(item: 10)

let stringStack: Stack<String> = Stack()
stringStack.push(item: "Hello")

3) What is the difference between Generic and Any?

Answer:

  • Generic follows Swift’s Type Safety
  • Any doesn’t follow type safety and you need to type check every time you perform an operation.
/** After Refactoring, Stack can hold multiple types **/
class Stack {

	var collection: [Any] = []

	func push(item: Any) {
			collection.append(item)
	}

	func pop() {
			collection.removeLast()
  }

	func display() {
			print(collection)
	}

  func sum() {

    var sum: Int = 0
    for item in collection {
			sum = sum + Int(item) // Casting is needed..
		}
		print(sum)

	}

}

var intStack: Stack<Int> = Stack()
intStack.push(item: 10)
intStack.push(item: 20)
intStack.sum() // 30

4) What are the advantages of Generics?

Answer:

  • Generic avoids writing duplicate code while achieving Swift’s Type Safety Feature.

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