I don't understand the swift syntax [] [].

Question:

func getWeatherString(local:Area) -> String
{
   return ["晴れ","曇り","雨"][local.weather]
}

I don't understand the syntax of return ["晴れ","曇り","雨"][local.weather] above.
How does this work?
I don't know the keyword even if I try to look it up on the net.

Answer: Answer:

return ["晴れ","曇り","雨"][local.weather]

Let's break this formula down into multiple formulas and rewrite it. Then it may be easier to understand.

let number: Int = local.weather
let weathers: [String] = ["晴れ","曇り","雨"]
let theWeather: String = weathers[number]
return theWeather

From the array, you can see that it is a process of extracting elements using subscripts.

Scroll to Top