I have been stuck for the last few days on this and have read countless posts here on Stackoverflow and across the web, but am still a little lost. (by the way I am a bit of a newbie in Swift).
i have the following code
Alamofire.request(.GET, "url")
.response { (request, response, data, error) in
printIn(data)}
This prints out a long string of numbers in the Console, which is perfect, exactly what I need.
However I now would like to iterate through these and get the number at certain index's, so would like to convert this into a string or NSData.
I have tried many different ways but have not yet found how to do this, if somebody could please help me I would be very grateful.
I have tried using
Alamofire.request(.GET, "url")
.responseString(encoding: NSASCIIStringEncoding) { (request, response, data, error) -> Void in
println(data)
}
but this only prints out a jumbled up mess.
many thanks
Chris
You say:
However I now would like to iterate through these and get the number at certain index's, so would like to convert this into a string or
NSData
.
When you use response
, the data
parameter actually is a NSData
. So just cast the variable to the appropriate type, and you should be in business, e.g.:
Alamofire.request(.GET, urlString)
.response { (request, response, data, error) in
if let data = data as? NSData {
for i in 0 ..< data.length {
var byte: UInt8!
data.getBytes(&byte, range: NSMakeRange(i, 1))
print(String(format: "%02x ", byte))
}
}
}
In my example loop, just logging the hex string representation of the byte
variable, but it's a numeric value with which you can do whatever you'd like.
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