我在递归操作中计数有问题。我正在创建一个站点地图生成器,我想计算每个 URL 链接的次数。到目前为止,我在数大于二时遇到了麻烦!但至少它很重要。
这是我的代码:
- (void)parseURL:(NSURL*)URL
{
NSLog(@"Parsing URL: %@", URL);
// Check for URL in visited Array
if ([self.visitedURLs containsObject:URL]) {
NSLog(@"URL Already Visited...");
// Add 1 to the count of links to this URL
[[self.collectedURLs objectForKey:[URL absoluteString]] addToIncomingLinks];
NSLog(@"Updating Link Count... (%d)", [[self.collectedURLs objectForKey:[URL absoluteString]] incomingLinks]);
// Done parsing this URL, nothing else to do here...
} else {
... Filter URLs and other stuff ...
// Add URL as visited
[self.visitedURLs addObject:URL];
// Add filtered URL to Dictionary with link count of 1
[self.collectedURLs setObject:[[OBAURLData alloc] init] forKey:workingURL];
// Reload Table
[self.crawlTableView reloadData];
// parse found URLs
[self parseURL:[NSURL URLWithString:workingURL]];
}
}
// From the OBAURLData Object
- (id)init
{
if ((self = [super init])) {
self.incomingLinks = 1;
}
return self;
}
- (void)addToIncomingLinks
{
self.incomingLinks = self.incomingLinks + 1;
}
所有代码都按预期工作,除了计数不高于 2,即使 NSLog 语句都显示 URL 被访问了两次以上。
这可能是因为递归还是我只是没有看到我的问题?