swift array optional to string

swift array optional to string

try these and see:

// According to your current result
for arrayElement in row {
  print(arrayElement)

  if let arrayString = arrayElement.first {
    print(arrayString)
  }

}

Try this for proper solution:

for childArray in row {
   print(childArray - (childArray))
   for stringValue in childArray {
       print(stringValue - (stringValue))
   }
}

Here is tested solution

let row = [
 [Optional(Lion Air)],
 [Optional(Malindo Air)]
]


row.forEach { (childArray) in
    childArray.forEach({ (optinalString) in
        print(optinalString - (String(describing: optinalString)))

        if let unoptionalString = optinalString {
            print(unoptionalString - (unoptionalString))
        }
    })
}

Result:

enter

Try this, using flatMap

Here is example optional string array to string array using flatMap

let optarray = [Optional(Swift),Optional(Java),Optional(C), nil]
let stringArray = optarray.flatMap{$0}
print(stringArray)

Output

[Swift, Java, C]

swift array optional to string

Leave a Reply

Your email address will not be published. Required fields are marked *