Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert value of type 'subSequence' (aka 'String.CharacterView') to type 'String' in collection

Tags:

string

swift

I build a webservis that requesting image url from the database. And I want to show it on the swift. But I'm gettig this error on the var photo line:

Cannot convert value of type 'subSequence' (aka 'String.CharacterView') to type 'String' in collection

let requestResponse = self.sendToServer(postUrl: "localhost",data:"abc")

let Seperated = requestResponse.characters.split(separator: " ")

var photo = Seperated[0] as String

let imageURL = URL(string: photo)
if let imageData = try? Data(contentsOf: imageURL!) {
    ...
}
like image 475
U. Demiroz Avatar asked May 19 '17 20:05

U. Demiroz


2 Answers

Consider something like this:

let requestResponse = "some string"
let separated = requestResponse.characters.split(separator: " ")

if let some = separated.first {
    let value = String(some)

    // Output: "some"
}
like image 123
Michael Avatar answered Nov 14 '22 14:11

Michael


The easiest solution.

let separated = requestResponse.characters.split(separator: " ").map { String($0) }
like image 33
Mickey Mouse Avatar answered Nov 14 '22 16:11

Mickey Mouse