0 votes
asked in Swift by (3.1k points)
Hi All

What is "is" in Swift language? and how to use?

1 Answer

0 votes
answered by (4.2k points)

  "is" is type checking operator in swift. It is used to check whether an instance is of a certain class type.

class MediaItem {

    var name: String

   init(name: String) {

      self.name = name




class Movie: MediaItem {

   var director: String

   init(name: String, director: String) {

      self.director = director

      super.init(name: name)




class Song: MediaItem {

   var artist: String

   init(name: String, artist: String) {

       self.artist = artist

       super.init(name: name)



let library = [

    Movie(name: "Casablanca", director: "Michael Curtiz"),

    Song(name: "Blue Suede Shoes", artist: "Elvis Presley"),

    Movie(name: "Citizen Kane", director: "Orson Welles"),

    Song(name: "The One And Only", artist: "Chesney Hawkes"),

    Song(name: "Never Gonna Give You Up", artist: "Rick Astley")


var movieCount = 0

var songCount = 0

for item in library {

    if item is Movie {


   } else if item is Song {