1

I want to combine arrays, but the join I want is as follows: I want the items of the Arrays that I want to combine. For example, I want to create a new array by combining the first item of ImageBannerLogo with the first item of ImageBannerLogo1 and the first item of ImageBannerLogo2.

    var ımageBannerLogo = [String]()
    var ımageBannerLogo1 = [String]()
    var ımageBannerLogo2 = [String]()
    var ımageBannerLogoAll = [String]()

override func viewDidLoad() {
        super.viewDidLoad()
ımageBannerLogo.append("one", "two", "three")
ımageBannerLogo1.append("1", "2", "3")
ımageBannerLogo2.append("4", "5", "6")

ımageBannerLogoAll.append(ımageBannerLogo+ ımageBannerLogo1+ ımageBannerLogo2)
}

I want its output to be as follows: ımageBannerLogoAll = "one14","two25","three36"

3 Answers 3

1
var ımageBannerLogo = ["one", "two", "three"]
var ımageBannerLogo1 = ["1", "2", "3"]
var ımageBannerLogo2 = ["4", "5", "6"]
var ımageBannerLogoAll = [String]()

Just iterate and format the string

    func loopIT() {
       //Include this check to avoid crash - array index out if range 
       guard ımageBannerLogo.count == ımageBannerLogo1.count && ımageBannerLogo.count == ımageBannerLogo2.count else { return }

       for i in 0..<ımageBannerLogo.count {
            ımageBannerLogoAll.append(String(format: "%@%@%@", ımageBannerLogo[i], ımageBannerLogo1[i], ımageBannerLogo2[i]))
       }

       print(ımageBannerLogoAll)
       //["one14", "two25", "three36"]
      }
Sign up to request clarification or add additional context in comments.

Comments

1
    var ımageBannerLogo:[String] = ["one", "two", "three"]
    var ımageBannerLogo1:[String] = ["1", "2", "3"]
    var ımageBannerLogo2 :[String] = ["4", "5", "6"]
    var ımageBannerLogoAll:[String] = []


for i in 0..<ımageBannerLogo.count{
    ımageBannerLogoAll.append("\(ımageBannerLogo[i])\(ımageBannerLogo1[i])\(ımageBannerLogo2[i])")
}


print(ımageBannerLogoAll)
//Result : ["one14", "two25", "three36"]

Comments

1

You need to append a sequence to an array this way. (See append(contentsOf:) doc)

ımageBannerLogo.append(contentsOf: ["one", "two", "three"])
ımageBannerLogo1.append(contentsOf: ["1", "2", "3"])
ımageBannerLogo2.append(contentsOf: ["4", "5", "6"])

You can then put them together with for loop like this.

for i in 0..<ımageBannerLogo.count {
    ımageBannerLogoAll.append(ımageBannerLogo[i] + ımageBannerLogo1[i] + ımageBannerLogo2[i])
}

Or, use zip and map like this.

ımageBannerLogoAll += zip(zip(ımageBannerLogo, ımageBannerLogo1), ımageBannerLogo2).map { $0.0 + $0.1 + $1 }

1 Comment

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.