I'm a longtime Obj-C developer but am new to Swift, like everyone else, and scratching my head at this one. I'm trying to convert an older tutorial that was written in Obj-C.
I have an "Animal" class declared like so:
class Animal
{
let title: String
let image: UIImage
let creator: String
init(title: String, image: UIImage, creator: String)
{
self.title = title
self.image = image
self.creator = creator
}
}
And in another class, I want an array of Animal instances, which is declared as a property of that class like so:
let animals: [Animal]
And initialized (unsuccessfully) like so:
animals = [Animal(title: "Sleeping Cat", image: UIImage.imageNamed("ID-101.jpg")
creator:"papaija2008"),
...
...
]
I am getting a build error in the first line of this array initializer, which reads just like the title of the post: "Type String does not conform to protocol StringLiteralConvertible". Can we not assign string literals to String objects? That seems impossible. What rule am I violating and how do I fix this?
Thanks in advance!
Using UIImage(named:) instead of UIImage.imageNamed() works for me:
let animals: [Animal] = [Animal(title: "Sleeping Cat", image: UIImage(named: "ID-101.jpg"), creator:"papaija2008")]
I hope it works for you too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With