Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Type'Program' does not conform to protocol 'Any Object'"

I updated xcode and now I have error in my project and I dont have idea what to do with it.

struct Program {
    let name : String
    let url : String
}

self.arrayOfPrograms = [Program(name: "First", url: "http://1.com"), Program(name: "Second", url: "http://2.com"), Program(name: "Third", url: "http://2.com")]

and I'm getting error "Type'Program' does not conform to protocol 'Any Object'"

like image 415
patrikbelis Avatar asked Feb 10 '26 14:02

patrikbelis


1 Answers

As reported in the documentation:

AnyObject can represent an instance of any class type.

A struct is not a class, so it cannot be cast to AnyObject

You should either:

  • turn Program into a class
  • define your array as Array<Any>
  • if your array is supposed to hold instances of Program only, declare it as Array<Program>

Needless to say, the last is the best solution, whereas the first is the one I wouldn't recommend because it requires you to make design changes (there's a reason why you declared it as a value type and not a reference type).

Side note: arrays and dictionaries can be cast to AnyObject because they are automatically bridged respectively to NSArray and NSDictionary, which are classes.

like image 71
Antonio Avatar answered Feb 13 '26 14:02

Antonio