我解析XML文件。当我在数组中添加值时,数组仍为null,我不知道原因。可能是因为我在两个班级中使用这个数组。 有.h文件
xmlparser.h
#import <UIKit/UIKit.h>
@class SightsTableViewController, Sight;
@interface XMLParser : NSObject {
NSMutableString *currentElementValue;
NSMutableArray *allSights;
NSMutableArray *Arr;
SightsTableViewController *sightsDelegate;
Sight *aSight;
}
- (XMLParser *) initXMLParser;
- (XMLParser *) arrayResult;
@property (nonatomic, retain) NSMutableArray *allSights;
@end
SightsTableViewController.h
#import <UIKit/UIKit.h>
@class SightsTableViewController, SightsDetailController;
@interface SightsTableViewController : UITableViewController <UITableViewDelegate>, UITableViewDataSource> {
IBOutlet UITableView *sightsTableView;
NSMutableArray *allSights;
NSMutableArray *Arr;
SightsDetailController *sightsDetailController;
SightsTableViewController *sightsDelegate;
}
@property (nonatomic, retain) NSMutableArray *allSights;
@property (nonatomic, retain) NSMutableArray *Arr;
@property (nonatomic, retain) SightsDetailController *sightsDetailController;
@end
和.m文件
我只发布使用数组的方法。 xmlparser.m
#import "XMLParser.h"
#import "SightsTableViewController.h"
#import "Sight.h"
@implementation XMLParser
@synthesize allSights;
- (XMLParser *) arrayResult {
Arr = [[NSMutableArray alloc] init];
[Arr addObject:@"fisrt"];
[Arr addObject:@"two"];
[Arr addObject:@"three"];
[Arr addObject:@"four"];
[Arr addObject:@"five"];
NSLog(@"%@", Arr);
return Arr;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"Sights"]) {
//Initialize the array.
sightsDelegate.allSights = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"Sight"]) {
//Initialize the sight.
aSight = [[Sight alloc] init];
//Extract the attribute here.
aSight.sightID = [[attributeDict objectForKey:@"id"] integerValue];
NSLog(@"Reading id value :%i", aSight.sightID);
}
NSLog(@"Processing Element: %@", elementName);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"Sights"])
return;
//There is nothing to do if we encounter the Sight element here.
//If we encounter the Sight element howevere, we want to add the sight object to the array
// and release the object.
if([elementName isEqualToString:@"Sight"]) {
//adding values
[allSights addObject:aSight];
[aSight release];
aSight = nil;
}
else
[aSight setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
- (void) dealloc {
[Arr release];
[aSight release];
[allSights release];
[super dealloc];
}
@end
SightsTableViewController.m
#import "SightsTableViewController.h"
#import "SightsDetailController.h"
#import "XMLParser.h"
#import "Sight.h"
@implementation SightsTableViewController
@synthesize sightsDetailController, allSights, Arr;
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSURL alloc] initWithString:@"http://onsnab.ru/sights.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
NSLog(@"No Errors");
else
NSLog(@"Error Error Error!!!");
[[XMLParser alloc] arrayResult];
//showing NULL :(
NSLog(@"%@", sightsDelegate.allSights);
self.navigationItem.title = @"Sights";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [sightsDelegate.allSights count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
NSString *cellValue = [sightsDelegate.allSights objectAtIndex:indexPath.row];
cell.text = cellValue;
// Set up the cell
return cell;
}
- (void)dealloc {
[sightsDelegate release];
[sightsDetailController release];
[super dealloc];
}
@end
答案 0 :(得分:1)
你的代码有点混乱,所以我很难阅读它。我的评论是关于NSArray和Objective-C如何比编写代码的方式更有效。
如果NSArray引用变量从未初始化,则它将为nil。您可以将Objective-C中的消息发送给nil参考,Objective-C不会抱怨。它什么都不做。这种行为在某些情况下非常方便,但在其他情况下则相当严重。
要检查您的NSArray是否未被初始化,请在初始化NSArray的代码行上放置一个断点。然后运行你的应用程序如果它永远不会在断点处停止,那么你就找到了你的错误。
答案 1 :(得分:1)
根据我的经验,当我有神秘的数组和字典时,我不会接受任何我放入的值,我总是可以追溯到没有初始化的结构。发送到nil对象(包括-addObject)的任何消息都是静默无操作。因此,如果事情无声无息,那么首先要怀疑的是,你正在和零谈话。
编辑:
例如,假设你有一个视图控制器,它允许你在UITextField中输入文本,并将你放在那里的字符串添加到NSMutableArray中。所以你可能会去:
MyViewController.h:
@implementation MyViewController : UIViewController {
UILabel *textInput;
NSMutableArray *enteredStrings;
}
@property (nonatomic, retain) IBOutlet UITextField *textInput;
@property (nonatomic, retain) NSMutableArray *enteredStrings;
-(IBAction)addTextToArray:(id *)sender
@end
然后MyViewController.m:
@implementation MyViewController
@synthesize textInput;
@synthesize enteredStrings;
-(void)viewDidLoad
{
//notice the lack of "self.enteredStrings = [NSMutableArray array]" right here!
}
-(IBAction)addTextToArray:(id *)sender
{
[self.enteredStrings addObject:self.textInput.text];
}
因此。最后一行不会给你任何错误,但它也行不通。 [self.enteredStrings count]
将顽固地保留在0.因为该属性指向nil指针,而不是NSMutableArray对象。