[英]How to convert NSData to Data in Swift 3?
I'm trying to create a simple weather app that grabs the user's location and shows simple weather data using the Google Maps api.我正在尝试创建一个简单的天气应用程序,它可以使用 Google Maps api 获取用户的位置并显示简单的天气数据。 Everything is working, except for this part where I take the JSON and get the address.
一切正常,除了我获取 JSON 并获取地址的这一部分。
func getAddressForLatLng(latitude: String, longitude: String) {
let url = NSURL(string: "\(baseUrl)latlng=\(latitude),\(longitude)&key=\(apikey)")
let data = NSData(contentsOf: url! as URL)
let json = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! Dictionary
if let result = json["results"] as? Dictionary {
if let address = result[0]["address_components"] as? Array {
let number = address[0]["short_name"] as! String
let street = address[1]["short_name"] as! String
let city = address[2]["short_name"] as! String
let state = address[4]["short_name"] as! String
let zip = address[6]["short_name"] as! String
weatherDisplay.text = "\(city),\(state)"
}
}
}
On the line:在线上:
let json = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! Dictionary
I get this error:我收到此错误:
Cannot invoke 'jsonObject' with an argument list of type '(with: NSData?, options: JSONSerialization.ReadingOptions)'
What am I doing wrong?我究竟做错了什么?
let nsdata = NSData()
let data = Data(referencing: nsdata)
https://developer.apple.com/documentation/foundation/data/3126627-init https://developer.apple.com/documentation/foundation/data/3126627-init
You need to change a couple things.你需要改变一些事情。 First, you are using
NSData
.首先,您使用的是
NSData
。 You should be using the Swift type Data
.您应该使用 Swift 类型
Data
。 To convert from NSData?
从
NSData?
转换NSData?
to Data?
Data?
, just add as Data?
,只是添加
as Data?
to the end of the variable declaration.到变量声明的末尾。
Also, Your type is optional, but you can't pass in an optional type, so you need to unwrap it (using, in this example, if let data = data { /* stuff here */}
):此外,您的类型是可选的,但您不能传入可选类型,因此您需要解开它(在本例中,使用
if let data = data { /* stuff here */}
):
func getAddressForLatLng(latitude: String, longitude: String) {
let url = NSURL(string: "\(baseUrl)latlng=\(latitude),\(longitude)&key=\(apikey)")
let data = NSData(contentsOf: url! as URL) as Data? // <==== Added 'as Data?'
if let data = data { // <====== Added 'if let'
let json = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! Dictionary
if let result = json["results"] as? Dictionary {
if let address = result[0]["address_components"] as? Array {
let number = address[0]["short_name"] as! String
let street = address[1]["short_name"] as! String
let city = address[2]["short_name"] as! String
let state = address[4]["short_name"] as! String
let zip = address[6]["short_name"] as! String
weatherDisplay.text = "\(city),\(state)"
}
}
}
}
Update:更新:
Another thing you need to change is:你需要改变的另一件事是:
let json = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! Dictionary
When you cast to the type Dictionary
, the compiler does not know what you are talking about because Dictionary
is a generic type .当您转换为
Dictionary
类型时,编译器不知道您在说什么,因为Dictionary
是泛型类型。 So you need to cast to Dictionary<String, AnyObject>
or [String: AnyObject]
(They are the same).所以你需要转换为
Dictionary<String, AnyObject>
或[String: AnyObject]
(它们是一样的)。
只需投射: let newData = nsData as Data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.