0

I'm not sure if this is even possible but lets say we have an array of strings and need to match them up with a corresponding array of images. Like :

["Patriots", "Broncos", "Cowboys"]  =  [pat.png, bronc.png, cow.png]

I've tried with the map function but it doesn't seem to work:

let footballArray = ["Patriots", "Broncos", "Cowboys"].map({return [pat.png, bronc.png, cow.png] })

Any help solving this issue is appreciated!

6
  • 2
    Why don't you use a Dictionary? Commented Aug 19, 2017 at 18:36
  • I'm not entirely sure how to set something like that up Commented Aug 19, 2017 at 18:37
  • developer.apple.com/library/content/documentation/Swift/… Commented Aug 19, 2017 at 18:38
  • stackoverflow.com/questions/34927057/… But as Luk2302 suggested, you may want to review the basic collection types. Commented Aug 19, 2017 at 18:42
  • have you solved you problem? Commented Aug 28, 2017 at 6:27

4 Answers 4

1

You can create dictionary like this from both sequence.

let a = ["Patriots", "Broncos", "Cowboys"]
let b = ["pat.png", "bronc.png", "cow.png"]

var footballDict: [String : String] = [:]

zip(a, b).forEach { footballDict[$0] = $1 }
Sign up to request clarification or add additional context in comments.

Comments

0

You can try let footballArray = [String : String] = ["Patriots" : "pat.png", "Broncos" : "bronc.png", "Cowboys" : "cow.png"]

Or view here

Comments

0

this is how you can add the extension to your strings using .map

let footballArray = ["Patriots", "Broncos", "Cowboys"].map({ (value: String) -> String in
        return value + ".png"

Comments

0

If you're talking about how to combine two arrays into one, you can use zip:

struct Team {
    let teamName: String
    let imageName: String
}

let teamNames = ["Patriots", "Broncos", "Cowboys"]
let imageNames =  ["pat.png", "bronc.png", "cow.png"]

let teams = zip(teamNames, imageNames).map { (teamName, imageName) in 
    Team(teamName: teamName, imageName: imageName)
}

That yields an array of Team objects, built using elements from those two arrays.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.