I've currently got a string array and I want to convert this to a UInt8 array? All of the strings are UInt8's, however I can only retrieve it as a string. Any ideas?
let stringValues = "[185, 221, 199, 111, 152, 137, 41, 137, 223, 66, 75, 132]"
I have each item in the array split up into its individual part, I just don't know how to convert the string to UInt8.
Thanks
This works:
var stringValues = "[185, 221, 199, 111, 152, 137, 41, 137, 223, 66, 75, 132]"
stringValues = stringValues.stringByReplacingOccurrencesOfString("[", withString: "").stringByReplacingOccurrencesOfString("]", withString: "").stringByReplacingOccurrencesOfString(" ", withString: "")
var values = [UInt8]()
print(stringValues)
for item in stringValues.componentsSeparatedByString(",") {
if let value = UInt8(item) {
values.append(value)
}
}
let stringValues = "[185, 221, 199, 111, 152, 137, 41, 137, 223, 66, 75, 132]"
let uint8Values = stringValues.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString:" ,\"[]"))
.filter { !$0.isEmpty}.flatMap{ UInt8($0) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With