gpt4 book ai didi

ios - SimplePing Apple 代表不解雇

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:29 25 4
gpt4 key购买 nike

以下问题:

我实现了一个 Ping 对象:

@interface PingTest : NSObject <SimplePingDelegate>
@property (strong, nonatomic) SimplePing* ping;

我从 Apple 获得的 SimplePing:https://developer.apple.com/library/mac/samplecode/SimplePing/Introduction/Intro.html

现在我正尝试发送这样的 Ping:

@synthesize ping;

ping = [SimplePing simplePingWithHostName:PING_SERVER];
ping.delegate = self;
[ping start];

#pragma mark - SimplePingDelegates

- (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address {
}

- (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet
{
NSLog(@"didSendPacket");
}

- (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet error:(NSError *)error
{
NSLog(@"didFailToSendPacket");
}

- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet
{
NSLog(@"didReceivePingResponsePacket");
}

但是我的委托(delegate)方法没有被调用...有人知道为什么吗?!

编辑:出于某种原因在 SimplePing.m 中:

- (void)start
// See comment in header.
{
// If the user supplied us with an address, just start pinging that. Otherwise
// start a host resolution.

if (self->_hostAddress != nil) {
[self startWithHostAddress];
} else {
Boolean success;
CFHostClientContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};
CFStreamError streamError;

assert(self->_host == NULL);

self->_host = CFHostCreateWithName(NULL, (__bridge CFStringRef) self.hostName);
assert(self->_host != NULL);

CFHostSetClient(self->_host, HostResolveCallback, &context);

CFHostScheduleWithRunLoop(self->_host, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);

NSLog(@">CFHostStartInfoResolution");
success = CFHostStartInfoResolution(self->_host, kCFHostAddresses, &streamError);
NSLog(@"<CFHostStartInfoResolution");
if ( ! success ) {
[self didFailWithHostStreamError:streamError];
}
}
}

“HostResolveCallback”永远不会被调用....这就是我认为的问题...

最佳答案

ARC 正在释放 SimplePing 实例(在您的情况下为 *ping),而不是使用属性,而是像这样使用 iVar您还忘记将 sendPingWithData 添加到 didStartWithAddress 委托(delegate)方法。看看

@implementation PingTest
{
/*CHANGED*/
SimplePing *_pinger;
}

- (void) startPinger
{
/*CHANGED*/
ping = [SimplePing simplePingWithHostName:PING_SERVER];
ping.delegate = self;
[ping start];
}


#pragma mark - SimplePingDelegates

- (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address
{
/*CHANGED*/
[pinger sendPingWithData:nil];
}

- (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet
{
NSLog(@"didSendPacket");
/*CHANGED*/
//Capture time here if you want to measure the latency
}

- (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet error:(NSError *)error
{
NSLog(@"didFailToSendPacket");
}

- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet
{
NSLog(@"didReceivePingResponsePacket");

/*CHANGED*/
//Capturing time here again and comparing it with the one from didSentPacket will
// give you the latency
}

关于ios - SimplePing Apple 代表不解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22766499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com