32

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

1
  • 2
    Just initialize a new String with your substring s.append(String(subStr)) Commented Sep 22, 2017 at 6:19

2 Answers 2

47

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

Change:

s.append(subStr) 

To:

s.append(String(subStr)) 
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome - I was having difficulty figuring out how to convert a String.subsequence but this is simple and effective. Take my upvote
2

my two cents for serro in different context. I was trying to get an array of "String" splitting a string. "split" gives back "Substring", for efficiency reason (as per Swift.org litre).

So I ended up doing:

let buffer = "one,two,three" let rows = buffer.split(separator:",") let realStrings = rows.map { subString -> String in return String(subString) } print(realStrings) 

Ape can help someone else.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.