Reputation: 354
I am trying to split a string returned from a URL by ", " but when I attempt
var tagArray = [String]()
if response.responseObject != nil {
let data = response.responseObject as NSData
let str = NSString(data: data, encoding: NSUTF8StringEncoding)!
self.tagArray = str.componentsSeparatedByString(", ") as String
println("response: \(self.tagArray)") //prints the HTML of the page
}
The line self.tagArray = str.componentsSeparatedByString(", ") as String
throws the error "'String" is not convertible to '[(String)]'"
Anyone know how to properly split the string into the array?
Upvotes: 0
Views: 463
Reputation: 1985
The offending line is missing the []
in the cast -- you're casting to a String
when you need to be casting to an array of Strings
:
var tagArray = [String]()
if response.responseObject != nil {
let data = response.responseObject as NSData
let str = NSString(data: data, encoding: NSUTF8StringEncoding)!
self.tagArray = str.componentsSeparatedByString(", ") as [String]
println("response: \(self.tagArray)") //prints the HTML of the page
}
Upvotes: 1