无法在自定义类中获取/使用已解析的元素以在其他类上使用

时间:2016-11-18 16:37:00

标签: ios swift nsxmlparser

所以我一直在努力解决这个问题的时间超过我追踪的时间。

我已经解析了我需要的元素,但我无法弄清楚如何在其他类上调用它们。 这是我写的自定义类。

class SoapTipParser: NSObject, XMLParserDelegate, NSURLConnectionDelegate {

    var webData:NSMutableData = NSMutableData()
    var parser:XMLParser?
    var flag: Bool?
    var capturedString: String?
    var conteudos: [LoginCentralResponse?]? //tips
    var elementos: LoginCentralResponse? //tip
    var delegate: SoapTipParserDelegate?



    func processXmlResponse(data :NSData) {
        parser = XMLParser(data: webData as Data)
        parser!.delegate = self
        parser!.parse()

    }

    func loginCentral() {
        //Envelope com o request
        var soapMessage = "<soapenv:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:log='LoginCentral'><soapenv:Header/><soapenv:Body><log:LoginCentral soapenv:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'><Autenticacao xsi:type='urn:Autenticacao' xmlns:urn='urn:RouterBoxMobile'><ChaveIntegracao xsi:type='xsd:string'>sL8xlbkw2454kLx3i832190461000160Lxd5yV055sKc3gHx9346</ChaveIntegracao></Autenticacao><DadosLoginCentral xsi:type='urn:DadosLoginCentral' xmlns:urn='urn:RouterBoxMobile'><Usuario xsi:type='xsd:string'>wagner</Usuario><Senha xsi:type='xsd:string'>mudar123</Senha></DadosLoginCentral></log:LoginCentral></soapenv:Body></soapenv:Envelope>"


        //link como string
        let urlString = "https://desenvtest.routerbox.com.br/routerbox/ws_mobile/rbx_server_mobile.php?wsdl"

        let url = NSURL (string: urlString)

        //Pega, parsea, e le
        let theRequest = NSMutableURLRequest(url: url! as URL)

        let countElements = soapMessage.characters.count

        theRequest.addValue("text/xml", forHTTPHeaderField: "Content-Type")
        theRequest.addValue(String(countElements), forHTTPHeaderField: "Content_length")
        theRequest.httpMethod = "POST"
        theRequest.httpBody = soapMessage.data(using: String.Encoding.utf8, allowLossyConversion: false)

        let urlConnection = NSURLConnection(request: theRequest as URLRequest, delegate: self, startImmediately: true)
        urlConnection!.start()

        if (urlConnection != nil) {
            var webData : Void = NSMutableData.initialize()

        }

    }

    func connection(_ connection: NSURLConnection, didFailWithError error: Error) {


    }


    func connection(_ connection: NSURLConnection, didReceiveResponse response: URLResponse) {
        webData.length = 0;

    }

    func connection(_ connection: NSURLConnection, didReceiveData data: Data) {
        webData.append(data as Data)

    }

    func connectionDidFinishLoading(_ connection: NSURLConnection) {
        var xmlStr = NSString(data: webData as Data, encoding: String.Encoding.utf8.rawValue)
        print(xmlStr)
        processXmlResponse(data: webData)
    }

    //XML Parser
    func parserDidStartDocument(_ parser: XMLParser) {
        flag = false
        capturedString = ""
        conteudos = []

    }
    func parserDidEndDocument(_ parser: XMLParser) {

        print("count of tips is \(conteudos!.count)")

        for (index, conteudo1) in (conteudos?.enumerated())!  {
            print("CodigoCliente e: \(conteudo1?.CodigoCliente)")
            print("Permissoes são: \(conteudo1?.Permissoes)")

        }


    }

    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI:  String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
        flag = false
        capturedString = ""



        if elementName == "CodigoCliente" {
            flag = true
        }
        if elementName == "Permissoes" {
            flag = true
        }
        //print("a")
    }

    func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {

        flag = false


        if elementName == "CodigoCliente" {
            elementos?.CodigoCliente = capturedString
        }
        if elementName == "Permissoes" {
            elementos?.Permissoes = capturedString
        }

        //print("ak")
    }

    func parser(_ parser: XMLParser, foundCharacters string: String) {

        if flag! {
            capturedString = capturedString! + string
        }
        //print("akii")
    }

    func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {

    }

}

但是每次我尝试在下一个类上调用解析后的元素时,我都会得到nil结果。

1 个答案:

答案 0 :(得分:0)

好的,我想我明白了,你从来没有初始化elementos,所以这两行

elementos?.CodigoCliente = capturedString
elementos?.Permissoes = capturedString

都尝试设置为nil对象。没有任何反应。你必须初始化elementos属性,它应该工作。

例如,在定义中:

var elementos = LoginCentralResponse()

(如果有这样的初始化器。)