1

Im just confused in to why I get an error when I explicitly specify the type of: challenges. I know type inference can determine its an array. But why would get an error?

var walkingChallenges: [String] = ["Walk 3 miles a day", "Beat every day goal"]
var runningChallenges: [String] = ["Run 5 miles a day", "Beat everyday goal"]

var challenges: [String] = [walkingChallenges, runningChallenges]

error: cannot convert value of type string to expected element type string.

The right way is to simply write:

var challenges = [walkingChallenges, runningChallenges]

Thanks!

0

1 Answer 1

2

challenges needs to be of type [[String]] - it is an array of string arrays

You can either say

var challenges: [[String]] = [walkingChallenges, runningChallenges]

or

var challenges = [walkingChallenges, runningChallenges]

and let Swift infer the type

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.