Reputation: 11
So I have this problem where I can't convert an element of my array to a string
I have a string like this
var description:[String] =["blue","yellow","red"]
and I want to give one element of my array to another variable which is chosen by another integer like this
var pick:[Int] = 2
var chosen:[String] = description[pick]
it says Cannot assign value of type 'String' to type '[String]' and to fix it xcode suggests to do it like this
var chosen:[String] = [description[pick]]
now if I want to cast this variable to another one or give it to a function or whatever it will say Cannot assign value of type 'String' to type '[[String]]' please help.
Upvotes: 0
Views: 76
Reputation: 2591
Is required for variable chosen to be an array? Otherwise you could just:
var chosen: String = description[pick]
Upvotes: 0
Reputation: 77641
You are getting very confused here...
First...
var array = ["red", "yellow"]
Is an array of strings. Don't call it description. Call things what they are.
Second...
var pick: [Int]
Is declaring an array. Setting it = 2
doesn't make sense.
Change your last line to...
var chosen: String = array[pick]
In the above line using [String]
here is telling the system that you are getting an array of Strings. You're not. You're getting a String here.
Upvotes: 1