Cannot convert value of type Substring to expected argument type String - Swift 4

MBH picture MBH · Sep 22, 2017 · Viewed 16.5k times · Source

Trying to get substring of String and append it to array of Strings:

var stringToSplit = "TEST TEXT"
var s = [String]()
let subStr = anotherString[0 ..< 6]
s.append(subStr) // <---- HERE I GET THE ERROR

error desc

Answer

Rashwan L picture Rashwan L · Sep 22, 2017

As @Leo Dabus mentioned, you need to initialize a new String with your substring:

Change:

s.append(subStr)

To:

s.append(String(subStr))