I have been stuck for the last few days on this and have read countless posts here on * and across the web, but am still a little lost. (by the way I am a bit of a newbie in Swift).
最近几天我一直被困在这里,并在*和网络上阅读了无数的帖子,但我仍然有点失落。 (顺便说一句,我是Swift的新手)。
i have the following code
我有以下代码
Alamofire.request(.GET, "url") .response { (request, response, data, error) in printIn(data)}
print(data)中的Alamofire.request(.GET,“url”)。response {(请求,响应,数据,错误)}
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.
但是我现在想迭代这些并获取某个索引的数字,所以想将它转换为字符串或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
2 个解决方案
#1
4
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
.但是我现在想迭代这些并获取某个索引的数字,所以想将它转换为字符串或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.:
使用响应时,data参数实际上是NSData。因此,只需将变量强制转换为适当的类型,您应该开展业务,例如:
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.
在我的示例循环中,只记录字节变量的十六进制字符串表示,但它是一个数值,您可以使用它来执行任何您想要的操作。
#2
2
The data is NSData and ascii encoded (in your 2nd example)
数据是NSData和ascii编码(在你的第二个例子中)
let s = NSString(data: data, encoding: NSASCIIStringEncoding)
in the first case you don't specify an encoding and so it defaults to NSUTF8
在第一种情况下,您没有指定编码,因此它默认为NSUTF8
let s = NSString(data: data, encoding: NSUTF8StringEncoding)
#1
4
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
.但是我现在想迭代这些并获取某个索引的数字,所以想将它转换为字符串或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.:
使用响应时,data参数实际上是NSData。因此,只需将变量强制转换为适当的类型,您应该开展业务,例如:
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.
在我的示例循环中,只记录字节变量的十六进制字符串表示,但它是一个数值,您可以使用它来执行任何您想要的操作。
#2
2
The data is NSData and ascii encoded (in your 2nd example)
数据是NSData和ascii编码(在你的第二个例子中)
let s = NSString(data: data, encoding: NSASCIIStringEncoding)
in the first case you don't specify an encoding and so it defaults to NSUTF8
在第一种情况下,您没有指定编码,因此它默认为NSUTF8
let s = NSString(data: data, encoding: NSUTF8StringEncoding)