Rue Vitale
Rue Vitale

Reputation: 1901

How to cast to an array of objects?

In CourseViewController I have the following variable:

var courses =  [Course]()

declared at top

I try to segue to CourseViewController, by doing this

   let dc = segue.destination as! CourseListViewController
   dc.courses = items[tag] as! [Course]

Is that the right way to do so? "items[tag] as! [Course]" is this correct?

Upvotes: 0

Views: 69

Answers (1)

The Pedestrian
The Pedestrian

Reputation: 543

If you're sure that the item at that tag would surely be a [Course] then it's absolutely correct. If not, use an if-let or guard to avoid runtime exception

 if let courses = items[tag] as? [Course]{
    dc.courses = courses
 } 

Upvotes: 1

Related Questions