我需要通过SimplePing库在我的iOS应用程序中ping一个主机名。委托方法在 ping 后不触发?。start(( 被调用。
//code to actually ping
var pinger: SimplePing?
var canStartPinging = false
func ping() {
pinger = SimplePing(hostName: "www.apple.com")
pinger?.delegate = self
pinger?.start()
repeat {
print("Repeat loop (String(describing:
pinger?.hostName))")
if (canStartPinging) {
pinger?.send(with: nil)
}
RunLoop.current.run(mode: RunLoop.Mode.default, before:
(NSDate.distantFuture ))
} while(pinger != nil)
}
//SimplePingDelegate Methods below
extension WelcomeViewController: SimplePingDelegate {
private func simplePing(pinger: SimplePing!, didFailToSendPacket
packet: NSData!, error: NSError!) {
print("didFailToSendPacket")
}
private func simplePing(pinger: SimplePing, didFailWithError error:
NSError) {
print("didFailWithError")
}
private func simplePing(pinger: SimplePing!,
didReceivePingResponsePacket packet: NSData!) {
print("didReceivePingResponsePacket")
}
private func simplePing(pinger: SimplePing!,
didReceiveUnexpectedPacket packet: NSData!) {
print("didReceiveUnexpectedPacket")
}
private func simplePing(pinger: SimplePing!, didSendPacket packet:
NSData!) {
print("didSendPacket")
}
private func simplePing(pinger: SimplePing!, didStartWithAddress
address: NSData!) {
print("didStartWithAddress : (String(describing: address))")
canStartPinging = true
}
}
未收到来自委托的回调。循环运行,但没有任何反应。打印"Repeat loop Optional("www.apple.com"(",并且从不打印 didStartWithAddress 或 didFailWithError。
事实证明,
委托方法的方法签名不正确。填充委托方法的最佳方法是开始键入 simplePing 并允许 Xcode 建议方法完成选项!