嗨,我正在使用NSUrlSession在我的Swift编程中调用一个Web服务,而且我对Swift编程语言越来越陌生。
在下面的程序中,我使用BackGroundClass来集成webservices,在得到结果之后,我使用协议来接收ViewController中的响应,但是那些协议函数没有在ViewController中调用,可以有人帮助我。
视图控制器:-

import UIKit

class ViewController: UIViewController,sampleProtocal {

    override func viewDidLoad() {
        super.viewDidLoad()

        BackGroundClass.delegate = self

        let params = ["scancode":"KK03799-008", "UserName":"admin"] as Dictionary<String, String>
        let backGround=BackGroundClass();
        backGround.callPostService("my Url", parameters: params)

    }

    func getResponse(result: NSDictionary) {
        print("Final response is\(result)");
    }

    func getErrorResponse(error: NSString) {
        print("Final Eroor code is\(error)")
    }
}

背景类:-
import UIKit

protocol sampleProtocal :  NSObjectProtocol{

    func getResponse(result:NSDictionary)
    func getErrorResponse(error:NSString)
}

class BackGroundClass: NSObject {

   var delegate:sampleProtocal?

    func callPostService(url:String,parameters:NSDictionary){


        print("url is===>\(url)")

        let request = NSMutableURLRequest(URL: NSURL(string:url)!)

        let session = NSURLSession.sharedSession()
        request.HTTPMethod = "POST"

        //Note : Add the corresponding "Content-Type" and "Accept" header. In this example I had used the application/json.
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")

        request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(parameters, options: [])

        let task = session.dataTaskWithRequest(request) { data, response, error in
            guard data != nil else {
                print("no data found: \(error)")
                return
            }

            do {
                if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
                    print("Response: \(json)")
                    self.mainResponse(json)
                } else {
                    let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)// No error thrown, but not NSDictionary
                    print("Error could not parse JSON: \(jsonStr)")
                    self.eroorResponse(jsonStr!)
                }
            } catch let parseError {
                print(parseError)// Log the error thrown by `JSONObjectWithData`
                let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
                print("Error could not parse JSON: '\(jsonStr)'")
                self.eroorResponse(jsonStr!)
            }
        }

        task.resume()
    }

    func mainResponse(result:NSDictionary){
        delegate?.getResponse(result)
    }

    func eroorResponse(result:NSString){
        delegate?.getErrorResponse(result)
    }
}

最佳答案

因为你没有指派代表

// remove this
//BackGroundClass.delegate = self

let params = ["scancode":"KK03799-008", "UserName":"admin"] as Dictionary<String, String>
let backGround=BackGroundClass();
// assign delegate here
backGround.delegate = self
backGround.callPostService("my Url", parameters: params)

关于ios - 协议(protocol)没有在快速的Ios中调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44918182/

10-11 17:14