UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How do you return or filter a dictionary [String : String]

Forums > Swift

I know my dictionary has values.

func loopthroughDic() { // DELETE
        weatherDictionary.forEach { print($0) }
    }

(key: "Tuesday", value: "Clear")
(key: "Thursday", value: "Rain")
(key: "Saturday", value: "Clear")
(key: "Wednesday", value: "Clouds")
(key: "Sunday", value: "Clear")
(key: "Friday", value: "Rain")
(key: "Monday", value: "Clouds")

Function that doesn't work. I am just sending "Monday" to it right now. Text("(WeatherListForcastViewModel.shared.displaySkyInfo(day: "Monday"))")

func displaySkyInfo(day: String) -> String {
        let matchedDay = weatherDictionary.filter { $0.key == day }
        print("Passed Day: \(day)")
        return matchedDay.values.first ?? ""
    }

Passed Day: Monday
Passed Day: Monday
Passed Day: Monday
Passed Day: Monday
Passed Day: Monday
Passed Day: Monday
Passed Day: Monday

Thanks for any assistance.

2      

And the above was based on this playground I created first which does work.

struct Weather {
    let days: [Double]
    let forcasts: [Forcast]
}

struct Forcast {
    let info: [String]
}

var weatherDetails = [Weather]()
weatherDetails.append(Weather(days: [1600362000.0], forcasts: [Forcast(info: ["Clouds"])]))
weatherDetails.append(Weather(days: [1600448400.0], forcasts: [Forcast(info: ["Rainy"])]))
weatherDetails.append(Weather(days: [1600534800.0], forcasts: [Forcast(info: ["Clear"])]))
weatherDetails.append(Weather(days: [1600621200.0], forcasts: [Forcast(info: ["Smoke"])]))
weatherDetails.append(Weather(days: [1600707600.0], forcasts: [Forcast(info: ["Clear"])]))
weatherDetails.append(Weather(days: [1600794000.0], forcasts: [Forcast(info: ["Clear"])]))
weatherDetails.append(Weather(days: [1600880400.0], forcasts: [Forcast(info: ["Smoke"])]))

let actualDays = ["Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday"]
var xDays = [String]()
var xForcast = [String]()
var xDictionary: [String : String] = [:]

func convertDate(timezone: Int, passedDate: Double) {
    let unixTimestamp = passedDate
    let date = Date(timeIntervalSince1970: unixTimestamp)
    let dateFormatter = DateFormatter()
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
    dateFormatter.dateFormat = "EEEE"
    dateFormatter.timeZone = TimeZone(secondsFromGMT: timezone)
    let strDate = dateFormatter.string(from: date)
    xDays.append(strDate)
}

weatherDetails.forEach { (item) in
    convertDate(timezone: -18000, passedDate: item.days[0])
}

weatherDetails.forEach { (item) in
    item.forcasts.forEach { (forcast) in
        xForcast.append(forcast.info[0])
    }
}

for (index, element) in xDays.enumerated() {
    xDictionary[element] = xForcast[index]
}

func getWeatherInfo(day: String) {
    let filtered = xDictionary.filter { $0.key == day }
    let theValue = filtered.values
    print("The Value \(day): \(theValue.first ?? "")")
}

xDictionary.forEach { print($0) }

getWeatherInfo(day: "Friday")

actualDays.forEach { (item) in
    getWeatherInfo(day: item)
}
(key: "Saturday", value: "Clear")
(key: "Wednesday", value: "Smoke")
(key: "Thursday", value: "Clouds")
(key: "Friday", value: "Rainy")
(key: "Monday", value: "Clear")
(key: "Tuesday", value: "Clear")
(key: "Sunday", value: "Smoke")
The Value Friday: Rainy
The Value Wednesday: Smoke
The Value Thursday: Clouds
The Value Friday: Rainy
The Value Saturday: Clear
The Value Sunday: Smoke
The Value Monday: Clear
The Value Tuesday: Clear

2      

You don't need to filter to get the value you need. Dictionary keys are hashable, meaning keys will be unique and you aren't going to have multiple values corresponding to the same key. So you can just do return weatherDictionary[day] to get the value you need.

let weatherDictionary: [String:String] = [
    "Tuesday": "Clear",
    "Thursday": "Rain",
    "Saturday": "Clear",
    "Wednesday": "Clouds",
    "Sunday": "Clear",
    "Friday": "Rain",
    "Monday": "Clouds",
]

func displaySkyInfo(day: String) -> String {
    print("Passed Day: \(day)")
    return weatherDictionary[day] ?? ""
}

let weather = displaySkyInfo(day: "Monday")
print(weather)

//Passed Day: Monday
//Clouds

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.