gpt4 book ai didi

ios - 如何在固定数量的项目后停止 rss 解析器?

转载 作者:行者123 更新时间:2023-11-29 12:20:34 24 4
gpt4 key购买 nike

我有一个 rss 解析器,我想在固定数量的项目后停止他,比如 5 或 10。我可以为没有在 arrat 中添加更多对象设置一个条件,但解析器仍然继续工作并占用内存。有人能帮我吗?谢谢你!

 - (void)parseXMLFileAtURL:(NSString *)URL
{

//you must then convert the path to a proper NSURL or it won't work
NSURL *xmlURL = [NSURL URLWithString:URL];

// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[rssParser setDelegate:self];

// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];

[rssParser parse];

}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{


currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];



}if ([elementName isEqualToString:@"enclosure"]){
NSString *imageStr = [NSString stringWithString:[attributeDict objectForKey:@"url"]];
[item setObject:imageStr forKey:@"_url"];
}

if ([elementName isEqualToString:@"media:thumbnail"]) {
[item setObject:currentPhoto forKey:@"_url"];}

}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"item"]) {
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentLink forKey:@"link"];
[item setObject:currentSummary forKey:@"description"];
[item setObject:currentDate forKey:@"pubDate"];
[item setObject:currentLogo forKey:@"logo"];


if (itemCount<5) {

[storiesNationale addObject:[item copy]];
itemCount++;
}
NSLog(@"adding story: %@", currentTitle);


}
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"found characters: %@", string);
// save the characters for the current item...
if ([currentElement isEqualToString:@"title"]) {
[currentTitle appendString:string];

} else if ([currentElement isEqualToString:@"link"]) {
[currentLink appendString:string];

} else if ([currentElement isEqualToString:@"description"]) {
[currentSummary appendString:string];

} else if ([currentElement isEqualToString:@"pubDate"]) {
[currentDate appendString:string];
}



}

最佳答案

请使用abortParsing 方法停止解析。

if (itemCount >= 10)
{
[parser abortParsing];
parset.delegate = nil;
}
else
{
itemCount++;
}

关于ios - 如何在固定数量的项目后停止 rss 解析器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30775125/

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