如何删除警告 "Conditional cast from 'Any' to 'AnyObject' always succeeds"
How to remove warning "Conditional cast from 'Any' to 'AnyObject' always succeeds"
我创建了一个 class,但当我尝试将对象转换为 AnyObject 时收到一条警告消息。
警告是:“从 'Any' 到 'AnyObject' 的条件转换总是成功的”
如何从我的文件中删除此警告?
这是我的代码:
class WebServices
{
class func getRequest( urlString: String, successBlock :@escaping (_ response :AnyObject)->Void, errorMsg:@escaping (_ errorMessage :String)->Void )
{
var request = URLRequest(url: URL(string: urlString)!)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) { (data, urlResponse, error) in
DispatchQueue.main.async {
if(error == nil)
{
do {
// Here is the warning
let responseJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? AnyObject
guard let _ = responseJSON else {
errorMsg("Some error has been occurred!")
return
}
successBlock(responseJSON!)
}
catch
{
errorMsg("Some error has been occurred!")
}
}
else
{
errorMsg(error!.localizedDescription)
}
}
}
task.resume()
}
}
感谢你们阅读本文!
这个函数
let responseJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? AnyObject
returns Any
并且你将它转换为 AnyObject
几乎相同,最好转换为预期的 json 内容无论是数组还是字典
let responseJSON = try JSONSerialization.jsonObject(with: data!) as? [String:Any]
或
let responseJSON = try JSONSerialization.jsonObject(with: data!) as? [Any]
并相应地更改完成
我创建了一个 class,但当我尝试将对象转换为 AnyObject 时收到一条警告消息。 警告是:“从 'Any' 到 'AnyObject' 的条件转换总是成功的” 如何从我的文件中删除此警告?
这是我的代码:
class WebServices
{
class func getRequest( urlString: String, successBlock :@escaping (_ response :AnyObject)->Void, errorMsg:@escaping (_ errorMessage :String)->Void )
{
var request = URLRequest(url: URL(string: urlString)!)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) { (data, urlResponse, error) in
DispatchQueue.main.async {
if(error == nil)
{
do {
// Here is the warning
let responseJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? AnyObject
guard let _ = responseJSON else {
errorMsg("Some error has been occurred!")
return
}
successBlock(responseJSON!)
}
catch
{
errorMsg("Some error has been occurred!")
}
}
else
{
errorMsg(error!.localizedDescription)
}
}
}
task.resume()
}
}
感谢你们阅读本文!
这个函数
let responseJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? AnyObject
returns Any
并且你将它转换为 AnyObject
几乎相同,最好转换为预期的 json 内容无论是数组还是字典
let responseJSON = try JSONSerialization.jsonObject(with: data!) as? [String:Any]
或
let responseJSON = try JSONSerialization.jsonObject(with: data!) as? [Any]
并相应地更改完成