Json Parsing Error: use of undeclared type 'Foundation' in Swift iOS
Json Parsing Error: use of undeclared type 'Foundation' in Swift iOS
我正在使用 Swift 创建一个应用程序,我必须在其中显示使用以下 URL 的国家/地区列表:
这里我只需要获取 name
键并添加到我的 NSMutableArray
。当我尝试使用 URLSession 解析它时,出现错误:
expression produced error: error: /var/folders/_3/27lhgzw9699c4_yg37r72_d80000gp/T/expr40-bcf47d..swift:1:65: error: use of undeclared type 'Foundation'
Swift._DebuggerSupport.stringForPrintObject(Swift.UnsafePointer<Foundation.Data>(bitPattern: 0x105c0c2f0)!.pointee)
下面是我的代码:
func fetchCountryList(countryURL:URL, completion:@escaping (NSDictionary) -> ()) {
print(countryURL)
let request = NSMutableURLRequest( url: countryURL as URL)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
do{
if let data = data,
let jsonString = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
, error == nil {
completion(jsonString)
} else {
print("error=\(error!.localizedDescription)")
let errorDict = ["error_status":true,"message":error!.localizedDescription] as [String : Any]
completion(errorDict as NSDictionary)
}
}
catch{
print("error=\(error.localizedDescription)")
let errorDict = ["error_status":true,"message":error.localizedDescription] as [String : Any]
completion(errorDict as NSDictionary)
}
}
task.resume()
}
任何人都可以在这里告诉我我的错误。提前致谢!
替换
let jsonString = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
和
if let result = try JSONSerialization.jsonObject(with: data, options:[]) as? [[String:Any]] {
for item in result {
print(item["name"])
}
}
响应是数组而不是字典也不要使用 NS 东西
NSDictionary => [String:Any]
NSUrl => URL
NSMutableURLRequest => URLRequest
func fetchCountryList(countryURL:URL, completion:@escaping (_ arr :[[String:Any]]?, _ error:Error?) -> ()) {
print(countryURL)
let request = URLRequest( url: countryURL)
let task = URLSession.shared.dataTask(with: request) {
data, response, error in
do{
if let data = data,
let jsonString = try JSONSerialization.jsonObject(with: data, options:[]) as? [[String:Any]]
, error == nil {
completion(jsonString,nil)
} else {
print("error=\(error!.localizedDescription)")
let errorDict = ["error_status":true,"message":error!.localizedDescription] as [String : Any]
completion(nil,error)
}
}
catch{
print("error=\(error.localizedDescription)")
let errorDict = ["error_status":true,"message":error.localizedDescription] as [String : Any]
completion(nil,error)
}
}
task.resume()
}
致电
serviceModel.fetchCountryList(countryURL:countryURL){ (result,error) in
if let res = result {
print(res)
}
}
替换
try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
有
try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [[String: Any]]
因为它是一个对象数组,我们还需要将 json 转换为 array
要获得 array of country names
,我们可以通过以下方式实现:
guard let countries = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [[String: Any]] else {
return
}
let countryNames = countries.map { [=12=]["name"] as! String }
我正在使用 Swift 创建一个应用程序,我必须在其中显示使用以下 URL 的国家/地区列表:
这里我只需要获取 name
键并添加到我的 NSMutableArray
。当我尝试使用 URLSession 解析它时,出现错误:
expression produced error: error: /var/folders/_3/27lhgzw9699c4_yg37r72_d80000gp/T/expr40-bcf47d..swift:1:65: error: use of undeclared type 'Foundation'
Swift._DebuggerSupport.stringForPrintObject(Swift.UnsafePointer<Foundation.Data>(bitPattern: 0x105c0c2f0)!.pointee)
下面是我的代码:
func fetchCountryList(countryURL:URL, completion:@escaping (NSDictionary) -> ()) {
print(countryURL)
let request = NSMutableURLRequest( url: countryURL as URL)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
do{
if let data = data,
let jsonString = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
, error == nil {
completion(jsonString)
} else {
print("error=\(error!.localizedDescription)")
let errorDict = ["error_status":true,"message":error!.localizedDescription] as [String : Any]
completion(errorDict as NSDictionary)
}
}
catch{
print("error=\(error.localizedDescription)")
let errorDict = ["error_status":true,"message":error.localizedDescription] as [String : Any]
completion(errorDict as NSDictionary)
}
}
task.resume()
}
任何人都可以在这里告诉我我的错误。提前致谢!
替换
let jsonString = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
和
if let result = try JSONSerialization.jsonObject(with: data, options:[]) as? [[String:Any]] {
for item in result {
print(item["name"])
}
}
响应是数组而不是字典也不要使用 NS 东西
NSDictionary => [String:Any]
NSUrl => URL
NSMutableURLRequest => URLRequest
func fetchCountryList(countryURL:URL, completion:@escaping (_ arr :[[String:Any]]?, _ error:Error?) -> ()) {
print(countryURL)
let request = URLRequest( url: countryURL)
let task = URLSession.shared.dataTask(with: request) {
data, response, error in
do{
if let data = data,
let jsonString = try JSONSerialization.jsonObject(with: data, options:[]) as? [[String:Any]]
, error == nil {
completion(jsonString,nil)
} else {
print("error=\(error!.localizedDescription)")
let errorDict = ["error_status":true,"message":error!.localizedDescription] as [String : Any]
completion(nil,error)
}
}
catch{
print("error=\(error.localizedDescription)")
let errorDict = ["error_status":true,"message":error.localizedDescription] as [String : Any]
completion(nil,error)
}
}
task.resume()
}
致电
serviceModel.fetchCountryList(countryURL:countryURL){ (result,error) in
if let res = result {
print(res)
}
}
替换
try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
有
try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [[String: Any]]
因为它是一个对象数组,我们还需要将 json 转换为 array
要获得 array of country names
,我们可以通过以下方式实现:
guard let countries = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [[String: Any]] else {
return
}
let countryNames = countries.map { [=12=]["name"] as! String }