【问题标题】:Return the count of an NSMutableArray from an XML id number in Objective-C?从Objective-C中的XML id号返回NSMutableArray的计数?
【发布时间】:2012-04-26 16:18:31
【问题描述】:

我正在从一个 XML 文件构建一个 NSMutableArray,并且需要返回 XML 文件中每个驻留 ID 的计数。可以这样做吗?

<residents>
<resident id="1">
    <name>
        <first>Daffy</first>
        <last>Duck</last>
    </name>
</resident>

<resident id="2">
    <name>
        <first>Mickey</first>
        <last>Mouse</last>
    </name>
</resident>

等等……

我将使用类似下面的代码返回计数:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"Count = %i", [appDelegate.residents count]);
return [appDelegate.residents count];

有什么建议吗?

对于数组,在我的 AppDelegate.h 中有:

    @interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UIWindow *window;
    UINavigationController *navigationController;

    NSMutableArray *residents; 
}


@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSMutableArray *residents;

在 XMLAppDelegate.h 我使用:

    @interface XMLAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;
    UINavigationController *navigationController;

    NSMutableArray *residents;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSMutableArray *residents;

@end

【问题讨论】:

  • 请提供构建数组的代码。谢谢。
  • 我编辑了帖子以包含代码。
  • 不,你没有 :) 你如何填充数组?
  • 嗯,我猜这只是创建它?如何填充它?
  • 看了一点代码后,我意识到我实际上只需要返回 标签在 XML 文件中出现的次数。我实际上并不需要'id'部分。仍然不确定如何实现它...

标签: objective-c xml arrays count nsmutablearray


【解决方案1】:

您需要使用 NSXMLParser(和 NSXMLParserDelegate 分别)并将 XML 数据正确解析到 NSMutableArray 中,然后才能计算任何内容。此外,为了存储日常人的 XML 数据,您可能希望将信息放入 NSMutableDictionaries。

在 .h @interface 中,您需要创建 IVAR(仅作为示例):

 @interface YourObject : UIViewController <NSXMLParserDelegate> {
   NSXMLParser *parser;
   NSString *currentElement;
   NSMutableString *firstName;
   NSMutableString *lastName;
   NSMutableArray *residents;
 }

在 .m 代码中的某处,调用它:

   NSString *xmlLocationString = @"http://www.website.com/feed.xml";
   NSURL *xmlLocationURL = [NSURL URLWithString:xmlLocationString];
   parser = [[NSXMLParser alloc] initWithContentsOfURL:xmlLocationURL];
   [parser setDelegate:self];
   [parser setShouldProcessNamespaces:NO];
   [parser setShouldReportNamespacePrefixes:NO];
   [parser setShouldResolveExternalEntities:NO];
   [parser parse];

当然,设置代理方法,它实际上会将 XML 文档解析到 NSMutableArray 中:

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

  currentElement = nil;
  currentElement = [elementName copy];
  if ([elementName isEqualToString:@"day"]) {
    firstName = [[NSMutableString alloc]init];
    lastName = [[NSMutableString alloc]init];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

   string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
   string = [string stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]];

   if ([currentElement isEqualToString:@"first"]) {
    [firstName appendString:string];
   } else if ([currentElement isEqualToString:@"last"]) {
    [lastName appendString:string];
   } else {
     ...
   }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{   
      if ([elementName isEqualToString:@"day"]) {
        NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
        [dictionary setObject:firstName forKey:@"fname"];
        [dictionary setObject:lastName forKey:@"lname"];
        //And finally
        [residents addObject:dictionary];
      }
}

然后,您可以在代码中的任何位置调用

[residents count]; 

获取居民总数。

注意:我只是边走边写这段代码,它可能有一些错误

【讨论】:

    【解决方案2】:

    据我了解,您只需要 .如果是这样,您可以执行以下操作。

    NSData *data = // your XML Data either from webservice or local file. Not xml string but the Data.
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    [parser setDelegate:self];
    [parser parse];
    

    此代码将是您获取数据的地方。在您的 .h 文件中:

    @interface yourViewController : UIViewController {
         NSInteger resedintsCount;
    }
    

    在您的 .m 文件中:

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ 
          if([elementName isEqualsToString:@"resident"]) {
               resedintsCount++;
          }
    }
    
    - (void)parserDidEndDocument:(NSXMLParser *)parser {
         // you can use resedintsCount 
    }
    

    我假设您在视图控制器中获取 XML 数据。如果您在 AppDelegate 或任何其他类中获得它,那么即使是相同的过程也会在那里。 不过,如果您有任何困惑,请随时提问。

    【讨论】:

    • NSXMLParser 还有很多其他的委托方法,但根据您的要求,您不需要它。如果您需要它们,您可以使用它们。参考NSXMLParserDelegate
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多