这不是一个真正的答案,但评论太长了。我看不出你发布的代码有什么问题。我将您的 getMoreProblems 代码粘贴到一个新项目中,并添加了查看结果所需的委托方法——据我所知,它运行良好。我可以在结果字符串中看到问题编号在我收到的第一页上以 1 开头(从第一次调用 getMoreProblems 开始),在第二次调用 getMoreProblems 时从问题 51 开始。我添加到您的 getMoreProblems 方法中的唯一内容是最后的 if-else 子句。这是我使用的代码:
@synthesize window = _window,receivedData;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
problemsPage = 0;
[self getMoreProblems];
}
-(void)getMoreProblems {
problemsPage++;
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://projecteuler.net/problems;page=%d",problemsPage]];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
NSLog(@"%p",conn);
conn=[[NSURLConnection alloc] initWithRequest:req delegate:self];
NSLog(@"%p",conn);
if (conn) {
self.receivedData = [NSMutableData data];
} else {
NSLog(@"The Connection Failed");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"%@",response.URL);
[self.receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"In connection:didReceiveData:");
[self.receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Succeeded! Received %lu bytes of data",[receivedData length]);
NSString *page = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
NSLog(@"%@",page);
[self performSelector:@selector(getMoreProblems) withObject:nil afterDelay:5];
}
所以,我无法重现你的问题——我猜它在你没有发布的一些代码中的其他地方。