0

I want to iterate over an array String?, String repeated pair but I cannot form the "for case let (a,b) in array" correctly.

The best I have come up with is to create a temp struct of {String?, String} and create an array of the temp structs and then iterate it but I would like to skip this step.

Below is the basic example with the last for loop showing the error Xcode reports.

class Foo {
    var s1: String?
    var s2: String?
    var s3: String?
}

let foo = Foo()
foo.s1="Test1"
foo.s2=nil
foo.s3="Test3"

let fooArray = [foo.s1, ", ", foo.s2, "; ", foo.s3,"."]
let fooArray1 = [foo.s1,foo.s2, foo.s3]
var text:String = ""
for case let prop? in fooArray1 {
    text = text + prop + " / "
}
print(text)
// The above works but now I want to use a different separator 
//base on the property name

text=""

for case let (prop, sep) in fooArray { // Error <= Expression Type 
// [String?] is ambiguous without more context

    text = text + prop + sep
}
print(text)

Here is what I have come up with

struct temp {
    var prop:String?
    var sep:String
    init(_ prop:String?, _ sep:String) {
        self.prop=prop
        self.sep=sep
}

let ary:[temp] = [ temp(foo.s1,", "), temp(foo.s2,"; "), temp(foo.s3,".") ]
text = ""
for a in ary {
    if let p = a.prop {
        text = text + p + a.sep
    }
}
print (text)

is there another way just using the for loop

for (a,b) in fooArray {
...
}
ielyamani
  • 17,807
  • 10
  • 55
  • 90
Richard Legault
  • 443
  • 4
  • 9
  • `fooArray` is not a array of (String, String). But `let fooArray = [(foo.s1, ", "), (foo.s2, "; "), (foo.s3,".")]` is a array of (String, String) – RJE Apr 27 '19 at 03:27

2 Answers2

0

As noted by @RJE, the inferred type of fooArray, as defined in your code, is [String?].

Here is one way to make it work:

class Foo {
    var s1: String?
    var s2: String?
    var s3: String?
}

let foo = Foo()
foo.s1 = "Test1"
foo.s2 = nil
foo.s3 = "Test3"

let fooArray1  = [foo.s1, foo.s2, foo.s3]
let separators = [", ", "; ", "."]

var text = ""

for i in fooArray1.indices {
    if let p = fooArray1[i] {
        text = text + p + separators[i]
    }
}

print (text)  //Test1, Test3.

Or

let zipped = zip(fooArray1, separators)

let text = zipped.map { tuple -> String in
    if case let (x?, y) = tuple {
        return x + y
    } else {
        return ""
    }
}.joined()

print (text)  //Test1,Test3.

Or

let fooArray = [foo.s1, ", ", foo.s2, "; ", foo.s3, "."]
var text = ""
var step = 1
var index = 0

while index < fooArray.count {
    if let str = fooArray[index] {
        step = 1
        text += str
    } else {
        step = 2
    }
    index += step
}

print(text) //Test1, Test3.

It would be better to define the initializer this way :

class Foo {
    var s1: String?
    var s2: String?
    var s3: String?

    init(s1: String?, s2: String?, s3: String?) {
        self.s1 = s1
        self.s2 = s2
        self.s3 = s3
    }
}

let foo = Foo(s1: "Test1", s2: nil, s3: "Test3")

P.S: The desired output seems to be more appropriate for a description property of the Foo class.

ielyamani
  • 17,807
  • 10
  • 55
  • 90
0

Thanks for the answer I was hoping through this question to get a better understanding of how to use [for] parameters. But the while solution is the solution I would probably use with the following modifications

text = ""
var index = 0 
while index < fooArray.count {    
    if let prop = fooArray[index] {
        index += 1
        let sep = fooArray[index]!
        index += 1
        text = text + prop + sep
    } else {
        index += 2
    }
}
Richard Legault
  • 443
  • 4
  • 9