2016-10-25 14 views
0

im is new to ios。私は石鹸リクエストを使用して簡単なログインアプリを作っています。 私はそれがスワイプでxml応答からjson配列を取得する方法

Optional(<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
     <CreateLoginResponse xmlns="http://tempuri.org/"> 
      <CreateLoginResult>[{"DelBoyId":36,"DelBoy_Fname":"abc-xyz","Password":"123","DelBoy_Email":"[email protected]","DelBoy_Contact":"123","RoleName":"Admin"}] 
      </CreateLoginResult> 
     </CreateLoginResponse> 
    </soap:Body> 
</soap:Envelope>). 

のように見えると私はセッション中にこのキー値[{"DelBoyId":36,"DelBoy_Fname":"abc-xyz","Password":"123","DelBoy_Email":"[email protected]","DelBoy_Contact":"123","RoleName":"Admin"}]を保存するサーバーからの応答を取得する際に問題があります。

どうすればいいですか?私を助けてください。

ありがとうございました。ここ

が私のコードである

import UIKit 

class LoginPageViewController: UIViewController, NSXMLParserDelegate { 

    @IBOutlet var txtUserEmail: UITextField! 

    @IBOutlet var txtUserPassword: UITextField! 

    var webData : NSMutableData? 
    var parser : NSXMLParser? 
    var flag : Bool? 
    var capturedString : String? 

    func processTerrorSaftyTips(data : NSData){ 
     parser = NSXMLParser(data: data) 
     parser!.delegate = self 
     parser!.parse() 

    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 

    } 


    @IBAction func loginButtonTapped(sender: AnyObject) { 

     let mobile = txtUserEmail.text; 
     let password = txtUserPassword.text; 


     let soapMessage = String(format: "<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http:/www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><CreateLogin xmlns='http://tempuri.org/'><mobile>"+mobile!+"</mobile><pasword>"+password!+"</pasword></CreateLogin></soap:Body></soap:Envelope>"); 

     let url = NSURL(string: "http://23.91.115.57/nagifts/NaGiftsWebService.asmx"); 

     let theRequest = NSMutableURLRequest (URL: url!); 
     let soapLength = String(soapMessage.characters.count) 

     theRequest.addValue("text/xml", forHTTPHeaderField: "Content-Type"); 
     theRequest.addValue(soapLength, forHTTPHeaderField: "Content-Length") 
     theRequest.HTTPMethod = "POST"; 
     theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion: false); 

     let urlConnection = NSURLConnection(request: theRequest , delegate: self); 

    } 

    func connection(connection: NSURLConnection, didFailWithError error: NSError){ 

    } 

    func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse){ 

     webData = NSMutableData() 
    } 
    func connection(connection: NSURLConnection, didReceiveData data: NSData){ 

     webData!.appendData(data) 
    } 
    func connectionDidFinishLoading(connection: NSURLConnection){ 

     processTerrorSaftyTips(webData!) 
    } 

    func parserDidStartDocument(parser: NSXMLParser){ 

     flag = false 
     capturedString = "" 
    } 
    func parserDidEndDocument(parser: NSXMLParser){ 

    } 

    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]){ 

     flag = false 
     capturedString = "" 
     if elementName == "CreateLoginResult" 
     { 
      flag = true 
     } 

    } 

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

     flag = false 
     if elementName == "CreateLoginResult" 
     { 
      print((capturedString!)) 


     } 
    } 
    func parser(parser: NSXMLParser, foundCharacters string: String){ 

     if flag! { 
      capturedString = capturedString! + string 
     } 

    } 
    func parser(parser: NSXMLParser, parseErrorOccurred parseError: NSError){ 

     } 

} 
+0

あなたはxcode8を使用していませんか? – Sanju

+0

no.currently arrayFromResult.add(capturedString) – Pratik

答えて

1

Swift3:

あなたの応答は、アレイに

var arrayFromResult = NSMutableArray() 

あるので、次はDidEndElementをパースし、配列にfoundCharactersを追加するために行くの任意の配列を宣言し、このように行います

if elementName == "CreateLoginResult" 
    { 
     print((capturedString!)) 
    arrayFromResult.add(capturedString) 

    } 

使用する配列または配列から辞書値を取得する

for var i in 0..<arrayFromResult.count { 

     let dictFromArray = arrayFromResult.object(at: i) as! NSDictionary 

     let name = dictFromArray.value(forKey: "DelBoy_Fname") as! String 
     print(name) 

     let password = dictFromArray.value(forKey: "Password") as! String 
     print(password) 


    } 
+0

が使用されています。arrayFromResult.add(capturedString).error occuresを追加すると "NSMutableArray型の値が加算されます" @Sanju ju – Pratik

+0

を追加すると、Xcode7.3 @sanju – Pratik

+0

tryこのように書く:arrayFromResult.add(capturedString as!NSMutableArray) – Sanju

関連する問題