Ios 在Swift中将NSMutableData转换为NSString

Ios 在Swift中将NSMutableData转换为NSString,ios,nsurlconnection,swift,Ios,Nsurlconnection,Swift,目前,我正在和斯威夫特一起玩 我想用NSURLConnection做一个小型下载程序。到目前为止一切正常,但我有两个问题 为什么我无法将响应数据转换为NSString 我如何将我的网站从nsurresponse转换为nshtpurlresponse 到目前为止,我的代码如下所示: import Foundation class Downloader: NSObject, NSURLConnectionDelegate { let urlString: String var r

目前,我正在和斯威夫特一起玩

我想用
NSURLConnection
做一个小型下载程序。到目前为止一切正常,但我有两个问题

  • 为什么我无法将响应数据转换为
    NSString
  • 我如何将我的网站从
    nsurresponse
    转换为
    nshtpurlresponse
  • 到目前为止,我的代码如下所示:

    import Foundation
    
    class Downloader: NSObject, NSURLConnectionDelegate {
    
        let urlString: String
        var responseData: NSMutableData = NSMutableData()
        var responseMessage: NSURLResponse = NSURLResponse()
    
        init(urlString: String) {
            self.urlString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding);
        }
    
        func startDownloaderWithUrl() {
            let url: NSURL = NSURL.URLWithString(self.urlString);
            let r: NSURLRequest = NSURLRequest(URL: url);
            let connection:NSURLConnection = NSURLConnection(
                request: r,
                delegate: self,
                startImmediately: true);
        }
    
        func connection(connection: NSURLConnection!, didFailWithError error: NSError!) {
            NSLog("Connection failed.\(error.localizedDescription)")
        }
    
        func connection(connection: NSURLConnection, didRecieveResponse response: NSURLResponse)  {
            NSLog("Recieved response")
            self.responseMessage = response;
        }
    
        func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
            self.responseData = NSMutableData()
        }
    
        func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
            self.responseData.appendData(data)
        }
    
        func connectionDidFinishLoading(connection: NSURLConnection!) {
            let responseString: NSString = NSString(data: self.responseData, encoding: NSUTF8StringEncoding);
    
            NSLog("%@", "Finished Loading");
            //NSLog("%@", self.responseData);
            NSLog("%@", responseString);
        }
     }
    

    我的
    responseString
    始终是
    nil
    ,尽管我的
    responseData
    有大量字节。第二,如何将响应消息转换为
    nshtpurlresponse

    将NSMutableData转换为字符串

    var YourSting = NSString(data responseData: NSData!, encoding encoding: UInt)
    

    将编码更改为ASCII对我很有效。

    试试看

    func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
        self.data = NSMutableData()
    }
    
    func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
        self.data.appendData(data)
    }
    
    func connectionDidFinishLoading(connection: NSURLConnection!) {
        var dta = NSString(data: data, encoding: NSUTF8StringEncoding)
        println("the string is: \(dta)")
    
        }
    

    我认为您的实现是正确的,请检查编码,而不是尝试
    NSASCIIStringEncoding
    Ok我使用的是错误的编码。NSASCIIStringEncoding按预期工作。但我确信在以前的纯Objective-C项目中,NSUTF8String编码对我是有效的。也许你知道如何将NSURLRepsonse转换为NSHTTPURLRResponse?我想它应该是这样的
    var urlRsponse=self.response消息作为NSHTTPURLResponse
    ?谢谢,但我使用的编码是错误的。我必须再等一天才能接受我自己的答案。