Learn Courses My Dashboard

Module 2: Lesson 5 Challenge

I completed this solution, but I had a doubt in here, what if, one has to select random class,

  1. how to fetch a random class from an array? Can you please help?
    code of the Challenge:
// Playground name: Module2 lesson 5 challange
// Made by: Sam-Grover
// Date: 20 - 05 - 22

import UIKit
import Foundation
var declaredArray: Array = ["Sam", "Tom", "Tina", "Bob"]
class Person
{
    var name: String = ""
    func introduceMyself()
    {
        print("Hi, my name is " + name)
    }
}
class Chef: Person
{
    override func introduceMyself() {
        print("Hi, my name is " + super.name)
        print("I am a Chef") //hardcorded String
    }
}
class Poet: Person
{
    override func introduceMyself() {
        print("Hi, my name is " + super.name)
        print("I am a Poet") //hardcorded String
    }
}
class Astranout: Person
{
    override func introduceMyself() {
        print("Hi, my name is " + super.name)
        print("I am an Astranout") //hardcorded String
    }
}
var a = 1
while(a <= 10)
{
    //Each iteration of the loop creates a new instance of one of your three subclasses
    let randomClass = Chef()
    let name = Int.random(in: 0...3)
    randomClass.name = declaredArray[name]
    randomClass.introduceMyself()
    a += 1
}