I have a list of items in an array. The default output of the items is a simple list separated by commas. However, a proper sentence would include the word "and" between the second last and last item in the list.
Swift code:
let myItem1: String = "Apple"
let myItem2: String = "Bee"
let myItem3: String = "Carrot"
let myItem4: String = "Dog"
let myArray = Set(arrayLiteral: myItem1, myItem2, myItem3, myItem4)
//print("Item count:", myArray.count)
print("Item list:", myArray.joinWithSeparator(", "))
Output:
The current output above is: "Apple, Dog, Carrot, Bee"
However, the proper output should be: "Apple, Dog, Carrot and Bee"
Question:
How can I modify the code so that the output includes the word "and" between the second last and last item in the list?
,as you do now. Add and & last item.myArray? Also you should never have to use theinit(xyzLiteral:)initialisers directly, they're meant for the compiler to use. In this case you can just dolet mySet : Set = [myItem1, myItem2, myItem3, myItem4]myArrayis better explained asmySet.arrayLiteralwas used because Xcode suggested it and was retuning an error prior to that.