我有一个NSOperation
子类,其中包含以下main
函数:
override func main() {
if self.cancelled {
return
}
var stringResponse: String!
var urlString: String!
if self.arduinoConnection.arduinoHTTPPort == 80 {
urlString = String(format: "http://%@/arduino/%@/%i/", arguments: [self.arduinoConnection.arduinoAddress, self.arduinoConnection.pinType.lowercaseString, self.arduinoConnection.arduinoPin])
} else {
urlString = String(format: "http://%@:%i/arduino/%@/%i/", arguments: [self.arduinoConnection.arduinoAddress, self.arduinoConnection.arduinoHTTPPort, self.arduinoConnection.pinType.lowercaseString, self.arduinoConnection.arduinoPin])
}
let url = NSURL(string: urlString)
let request = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
stringResponse = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
if (stringResponse == "") {
self.arduinoConnection.state = .Failed
self.arduinoConnection.switchState = false
self.arduinoConnection.statusText = "Failed To Connect"
} else {
self.arduinoConnection.state = .Downloaded
if (stringResponse == "0") {
self.arduinoConnection.state = .Downloaded
self.arduinoConnection.switchState = false
self.arduinoConnection.statusText = "Connected"
} else if (stringResponse == "1") {
self.arduinoConnection.state = .Downloaded
self.arduinoConnection.switchState = true
self.arduinoConnection.statusText = "Connected"
}
}
}
}
我的操作状态为finished
,NSURLConnection.sendAsynchronousRequest
的完成处理程序实际上已被破坏。如何使NSOperation
等到NSURLConnection.sendAsynchronousRequest
的完成处理程序触发?