ios - Concatenate String in Swift -
i have array contains strings i.e array
i tried concatenate string, got error "string not identical uint8"
var titlestring:string! = "" title in array { titlestring += "\(title)" }
to concatenate elements of string array, can utilize reduce
method:
var string = ["this", "is", "a", "string"] allow res = string.reduce("") { $0 + $1 }
the first parameter initial string, empty, , sec closure, executed each element in array. closure receives 2 parameters: value returned @ previous step (or initial value, if it's 1st element), , current element value.
more info here
addendum forgot explicitly reply question: concatenation doesn't work because declared titlestring
optional - turn non optional variable , work. if still want utilize optional, utilize forced unwrapping when doing assignment:
titlestring! += "\(title)"
addendum 2 suggested @martinr, there's simpler way concatenate:
join("", string)
ios iphone swift ios8
No comments:
Post a Comment