JSON Parsing and recieved : fatal error: unexpectedly found nil while unwrapping an Optional value
JSON Parsing and recieved : fatal error: unexpectedly found nil while unwrapping an Optional value
override func viewDidLoad() {
super.viewDidLoad()
let reposURL = NSURL(string: "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=AIzaSyC4BR5rZLNkeGChLr8N00BHZqBYqPdrgjg&location=43.690578,-79.342348&radius=400&types=cafe|bakery")
// 2
if let JSONData = NSData(contentsOfURL: reposURL!) {
// 3
if let json = NSJSONSerialization.JSONObjectWithData(JSONData, options: nil, error: nil) as? NSDictionary {
// 4
if let reposArray = json["items"] as? [NSDictionary] {
// 5
for item in reposArray {
repositories.append(Repository(json: item))
}
}
}
}
}
在第 5 行收到 exc_bad_instruction 错误,不知道为什么,它说我强制展开 nil 但 URL 正在工作,我可以在我的浏览器中毫无问题地打开它并查看所有内容JSON 数据
用 NSUTF8StringEncoding
编码你的 URL :
var urlstr = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=AIzaSyC4BR5rZLNkeGChLr8N00BHZqBYqPdrgjg&location=43.690578,-79.342348&radius=400&types=cafe|bakery"
var escapedString = urlstr.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let reposURL = NSURL(string: escapedString!)
// 2
if let JSONData = NSData(contentsOfURL: reposURL!) {
// 3
if let json = NSJSONSerialization.JSONObjectWithData(JSONData, options: nil, error: nil) as? NSDictionary {
// 4
print(json)
if let reposArray = json["results"] as? [NSDictionary] {
// 5
for item in reposArray {
print(item)// You will get particular items
}
}
}
}
然后您将收到 JSON 回复。
响应中没有 items
.
这样的参数
因此,在 print(json)
之后检查响应并通过参数获取数据。
希望对您有所帮助。
与 Swift 2 NSJSONSerialization 的签名已更改,以符合新的错误处理系统。这是与@ashish-kakkad 提出的相同答案,但已更新为 Swift 2
var urlstr = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=AIzaSyC4BR5rZLNkeGChLr8N00BHZqBYqPdrgjg&location=43.690578,-79.342348&radius=400&types=cafe|bakery"
var escapedString = urlstr.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let reposURL = NSURL(string: escapedString!)
// 2
if let JSONData = NSData(contentsOfURL: reposURL!) {
// 3
do {
if let json = try NSJSONSerialization.JSONObjectWithData(JSONData, options: []) as? NSDictionary {
// 4
print(json)
if let reposArray = json["results"] as? [NSDictionary] {
// 5
for item in reposArray {
print(item)// You will get particular items
}
}
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let reposURL = NSURL(string: "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=AIzaSyC4BR5rZLNkeGChLr8N00BHZqBYqPdrgjg&location=43.690578,-79.342348&radius=400&types=cafe|bakery")
// 2
if let JSONData = NSData(contentsOfURL: reposURL!) {
// 3
if let json = NSJSONSerialization.JSONObjectWithData(JSONData, options: nil, error: nil) as? NSDictionary {
// 4
if let reposArray = json["items"] as? [NSDictionary] {
// 5
for item in reposArray {
repositories.append(Repository(json: item))
}
}
}
}
}
在第 5 行收到 exc_bad_instruction 错误,不知道为什么,它说我强制展开 nil 但 URL 正在工作,我可以在我的浏览器中毫无问题地打开它并查看所有内容JSON 数据
用 NSUTF8StringEncoding
编码你的 URL :
var urlstr = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=AIzaSyC4BR5rZLNkeGChLr8N00BHZqBYqPdrgjg&location=43.690578,-79.342348&radius=400&types=cafe|bakery"
var escapedString = urlstr.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let reposURL = NSURL(string: escapedString!)
// 2
if let JSONData = NSData(contentsOfURL: reposURL!) {
// 3
if let json = NSJSONSerialization.JSONObjectWithData(JSONData, options: nil, error: nil) as? NSDictionary {
// 4
print(json)
if let reposArray = json["results"] as? [NSDictionary] {
// 5
for item in reposArray {
print(item)// You will get particular items
}
}
}
}
然后您将收到 JSON 回复。
响应中没有 items
.
因此,在 print(json)
之后检查响应并通过参数获取数据。
希望对您有所帮助。
与 Swift 2 NSJSONSerialization 的签名已更改,以符合新的错误处理系统。这是与@ashish-kakkad 提出的相同答案,但已更新为 Swift 2
var urlstr = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=AIzaSyC4BR5rZLNkeGChLr8N00BHZqBYqPdrgjg&location=43.690578,-79.342348&radius=400&types=cafe|bakery"
var escapedString = urlstr.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let reposURL = NSURL(string: escapedString!)
// 2
if let JSONData = NSData(contentsOfURL: reposURL!) {
// 3
do {
if let json = try NSJSONSerialization.JSONObjectWithData(JSONData, options: []) as? NSDictionary {
// 4
print(json)
if let reposArray = json["results"] as? [NSDictionary] {
// 5
for item in reposArray {
print(item)// You will get particular items
}
}
}
} catch let error as NSError {
print(error.localizedDescription)
}
}