我尝试的是在一个单独的类文件中的NetworkCall(Asyncronous call)。 当网络调用Get Completed时,它应该在我的实际ViewController中抛出一个函数callBack并更新一些UI-Views
我有一个像这样的FacilitiesNewVC.Swift类
class FacilitiesNewVC: UIViewController, RoomsDataProtocol {
var data = NSMutableData()
var roomsData = RoomsData()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//Image Background
roomsData.startConnection();
}
// Overriden Method (Not Called)
func didFinishLoadingUrl(jsonResult:NSDictionary) -> Void{
//This Method not getting called
println("In View Controller")
}
}
注意: roomsData.startConnection();
这里是RoomsData.Swift
class RoomsData: NSObject, NSURLConnectionDelegate {
var data = NSMutableData()
var callBack:RoomsDataProtocol?
func startConnection(){
let urlPath: String = "http://blue.genetechz.com/qadir/bed_room.php"
var url: NSURL = NSURL(string: urlPath)!
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
println("startConnection");
connection.start()
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
self.data.appendData(data)
println("connection");
}
func connectionDidFinishLoading(connection: NSURLConnection!) {
println("connectionDidFinishLoading");
var err: NSError
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
callBack?.didFinishLoadingUrl(jsonResult); // Call the Overriden Method in FacilitiesNewVC
println("RoomsData")
}
}
最后这是我的协议,即RoomsDataProtocol
protocol RoomsDataProtocol {
func didFinishLoadingUrl(jsonData:NSDictionary)
}
为什么我的FacilitiesNewVC.swift类中的override方法(即didFinishLoadingUrl())没有被调用?
答案 0 :(得分:0)
在您发布的代码中,没有任何行实际设置callBack
类中的RoomsData
变量。因此?.
运算符不会在行
callBack?.didFinishLoadingUrl(jsonResult);
我建议您添加
roomsData.callBack = self
在致电之前的某个地方
roomsData.startConnection();
在FacilitiesNewVC
课程中。