title: Soap service in Swift2
body: <p>I'm doing my updates for my Soap service with the new version of Swift, but I don't know how to resolve these errors:</p>
  
  <pre><code>class ViewController: UIViewController, UITextFieldDelegate, NSURLConnectionDelegate, NSXMLParserDelegate {
  
      var mutableData: NSMutableData = NSMutableData.alloc()
      var currentElementName: NSString = ""
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
  
          var soapMessage = "&lt;?xml version='1.0' encoding='UTF-8'?&gt; ...&lt;/SOAP-ENV:Envelope&gt;"
  
  
  
          var urlString = "https://..."
  
          var url = NSURL(string: urlString)
          var theRequest = NSMutableURLRequest(URL: url!)
          var msgLength = String(count(soapMessage))
  
          theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
          theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
          theRequest.addValue("urn:ResponseAction", forHTTPHeaderField: "SOAPAction")
          theRequest.HTTPMethod = "POST"
          theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
          var connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)
          connection?.start()
          if(connection == true) {
              var mutableData : Void = NSMutableData.initialize()
          }
      }
  
  
      func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
          mutableData.length = 0;
      }
  
      func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
          mutableData.appendData(data)
      }
  
  
      func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge)
      {
          challenge.sender.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust), forAuthenticationChallenge: challenge)
          challenge.sender.continueWithoutCredentialForAuthenticationChallenge(challenge)
      }
  
      func connectionDidFinishLoading(connection: NSURLConnection!) {
          let xmlParser = NSXMLParser(data: mutableData)
          xmlParser.delegate = self
          xmlParser.parse()
          xmlParser.shouldResolveExternalEntities = true
  
      }
  
      func parser(parser: NSXMLParser,
          didStartElement elementName: String,
          namespaceURI: String?,
          qualifiedName qName: String?,
          attributes attributeDict: [NSObject : AnyObject]) {
          currentElementName = elementName
      }
  
      func parser(parser: NSXMLParser, foundCharacters string: String) {
          if currentElementName == "LoginReturn" {
              print(string)
          }
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
  
  }
  </code></pre>
  
  <p>Errors:</p>
  
  <blockquote>
    <p>/Users/informatiqueresponis/Desktop/...
    2/SoapSwift/ViewController.swift:28:32: Cannot invoke 'count' with an
    argument list of type '(String)'</p>
    
    <p>/Users/informatiqueresponis/Desktop/...
    2/SoapSwift/ViewController.swift:66:10: Objective-C method
    'parser:didStartElement:namespaceURI:qualifiedName:attributes:'
    provided by method
    'parser(<em>:didStartElement:namespaceURI:qualifiedName:attributes:)'
    conflicts with optional requirement method
    'parser(</em>:didStartElement:namespaceURI:qualifiedName:attributes:)' in
    protocol 'NSXMLParserDelegate'</p>
    
    <p>/Users/informatiqueresponis/Desktop/...
    2/SoapSwift/ViewController.swift:13:52: 'alloc()' is unavailable in
    Swift: use object initializers instead</p>
    
    <p>/Users/informatiqueresponis/Desktop/...
    2/SoapSwift/ViewController.swift:54:19: Value of optional type
    'NSURLAuthenticationChallengeSender?' not unwrapped; did you mean to
    use '!' or '?'?</p>
    
    <p>/Users/informatiqueresponis/Desktop/...
    2/SoapSwift/ViewController.swift:54:92: Value of optional type
    'SecTrust?' not unwrapped; did you mean to use '!' or '?'?</p>
    
    <p>/Users/informatiqueresponis/Desktop/...
    2/SoapSwift/ViewController.swift:55:19: Value of optional type
    'NSURLAuthenticationChallengeSender?' not unwrapped; did you mean to
    use '!' or '?'?</p>
  </blockquote>
  
  <p>I did corrections of my code:</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController, UITextFieldDelegate, NSURLConnectionDelegate, NSXMLParserDelegate {
  
      var mutableData: NSMutableData = NSMutableData()
      var currentElementName: NSString = ""
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
  
          let soapMessage = "..."
  
  
  
          let urlString = "https://..."
  
          let url = NSURL(string: urlString)
          let theRequest = NSMutableURLRequest(URL: url!)
          let msgLength = String(soapMessage.characters.count)
  
          theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
          theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
          theRequest.addValue("urn:ResponseAction", forHTTPHeaderField: "SOAPAction")
          theRequest.HTTPMethod = "POST"
          theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
          let connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)
          connection?.start()
          if(connection == true) {
              let mutableData : Void = NSMutableData.initialize()
          }
      }
  
  
      func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
          mutableData.length = 0;
      }
  
      func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
          mutableData.appendData(data)
      }
  
  
      func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge)
      {
          challenge.sender!.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!), forAuthenticationChallenge: challenge)
          challenge.sender!.continueWithoutCredentialForAuthenticationChallenge(challenge)
      }
  
      func connectionDidFinishLoading(connection: NSURLConnection!) {
          let xmlParser = NSXMLParser(data: mutableData)
          xmlParser.delegate = self
          xmlParser.parse()
          xmlParser.shouldResolveExternalEntities = true
  
      }
  
      func parser(parser: NSXMLParser,
          didEndElement elementName: String,
          namespaceURI: String?,
          qualifiedName qName: String?,
          attributes attributeDict: [NSObject : AnyObject]) {
          currentElementName = elementName
      }
  
      func parser(parser: NSXMLParser, foundCharacters string: String) {
          if currentElementName == "LoginReturn" {
              print(string)
          }
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
  
  }
  </code></pre>
  
  <p>But when I run my app I get nothing</p>