如何将NSInlineData转换为字符串?

时间:2017-12-26 14:46:35

标签: ios swift afnetworking

在我的应用程序中,我实现了谷歌地图街景,所以我使用AFNetworking解析街景api,但响应对象是NSInline数据,无法转换为字符串请帮我获取对象..

编码部分是,

let manager = AFHTTPSessionManager()
    manager.requestSerializer = AFJSONRequestSerializer()
    manager.responseSerializer = AFHTTPResponseSerializer()
    manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/plain", "text/html", "application/json", "image/jpeg"]) as Set<NSObject> as Set<NSObject>! as! Set<String>?
    let url:NSString = NSString(format: "https://maps.googleapis.com/maps/api/streetview?size=400x300&location=29.812113,-95.441671&heading=151.78&pitch=-0.76&key=AIzaSyBzUYqzUts1fwZMG3EPGZyi3_aMr632HGJBH" as NSString)
    print(url)
    manager.get(url as String, parameters: nil, progress: nil, success: {
        (operation, responseObject) in
        let locStr:String = responseObject as! String . -->here error shows like could not cast value type nsinlinedata to string.
        print("locStr is:",locStr)
    }, failure: {
        (operation, error) in
        print(error)
    }) 

1 个答案:

答案 0 :(得分:1)

您的响应对象属于数据类型,请尝试:

if let data = responseObject  as? Data { 
    let dataStr = String(data: data, encoding: .utf8) 
}