从Objective-C中的XML ID号返回NSMutableArray的计数? [英] Return the count of an NSMutableArray from an XML id number in Objective-C?

查看:171
本文介绍了从Objective-C中的XML ID号返回NSMutableArray的计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从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>鼠标< / last>
< / name>
< / resident>



等...



使用类似于以下代码返回计数:

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

有任何建议吗?



,在我的AppDelegate.h我有:

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

NSMutableArray *居民;
}


@property(强,非原子)UIWindow * window;
@property(nonatomic,retain)IBOutlet UINavigationController * navigationController;
@property(nonatomic,retain)NSMutableArray *居民;

在XMLAppDelegate.h中,我使用:

  @interface XMLAppDelegate:NSObject< UIApplicationDelegate> {

UIWindow * window;
UINavigationController * navigationController;

NSMutableArray *居民;
}

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

@end


解决方案

的我明白你只需要数。如果是这样,您可以执行以下操作。

  NSData * data = //您的XML数据来自webservice或本地文件。不是xml字符串,而是数据。 
NSXMLParser * parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
[parser parse];

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

  @interface yourViewController:UIViewController {
NSInteger resedintsCount;
}

您的.m文件:


$ b (NSString *)nameName(NSString *)namespaceURI(NSString *)namespaceURI(NSString *)qName属性:(NSDictionary) *)attributeDict {
if([elementName isEqualsToString:@resident]){
resedintsCount ++;
}
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
//可以使用resedintsCount
}

我假设你在视图控制器中获取你的XML数据。如果你得到它在AppDelegate或任何其他类,甚至相同的过程将在那里。
仍然如果你有困惑,请随时问。


I am building an NSMutableArray out of an XML file and need to return the count of each resident id in the XML file. Is it possible to do this?

<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>

etc...

I will be returning the count using code similar to this:

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

Any suggestions?

For the array, in my AppDelegate.h I have:

    @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;

In XMLAppDelegate.h I use:

    @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

解决方案

As of I understand you only need the count of . If so you can do following.

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];

This code will be where you get your data. In your .h file:

@interface yourViewController : UIViewController {
     NSInteger resedintsCount;
}

in your .m File:

- (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 
}

I assumed that you are getting your XML data in view controller. If you are getting it in AppDelegate or any other class than even the same process will be there. Still if you have in confusion feel free to ask.

这篇关于从Objective-C中的XML ID号返回NSMutableArray的计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆