传递 NULL 值

发布于 2024-09-05 14:36:51 字数 3939 浏览 4 评论 0原文

我使用 NSXMLParser 的实例。我将找到的字符存储在 NSMutableStrings 中,这些字符存储在 NSMutableDictionary 中,然后将这些 Dict 添加到 NSMutableArray 中。

当我测试这个时,一切看起来都很正常:我计算了 1 个数组、x 个字典和 x 个字符串。

在详细视图控制器文件中,我想显示我的解析结果。我调用存储所有内容的类,但我得到 (null) 返回。

这就是我所做的(错误):

xAppDelegate.h

@interface xAppDelegate : NSObject <UIApplicationDelegate> {    
    UIWindow *window;
    UINavigationController *navigationController;
}

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

@end

xAppDelegate.m

#import "xAppDelegate.h"
#import "RootViewController.h"
#import "XMLParser.h"

@implementation xAppDelegate
@synthesize window, navigationController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // OFFLINE DOCUMENT > Resources folder
    NSString *Path = [[NSBundle mainBundle] bundlePath];
    NSString *DataPath = [Path stringByAppendingPathComponent:@"file.xml"];
    NSData *Data = [[NSData alloc] initWithContentsOfFile:DataPath];

    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:Data];       

    XMLParser *parser = [[XMLParser alloc] initXMLParser];

    [xmlParser setDelegate:parser]; 
    [xmlParser setShouldProcessNamespaces:NO];
    [xmlParser setShouldReportNamespacePrefixes:NO];
    [xmlParser setShouldResolveExternalEntities:NO];

    [xmlParser parse];      

    [window addSubview: navigationController.view];
    [window makeKeyAndVisible];
}

- (void)dealloc {
    [window release];
    [navigationController release];
    [super dealloc];
}
@end

    XMLParser.h
@class xAppDelegate;

    @interface XMLParser : NSObject {
        NSMutableArray *array;
        NSMUtableDictionary *dictionary;
        NSSMutabletring *element;
xAppDelegate *appDelegate;
    }
- (XMLParser *) initXMLParser;
    @property (nonatomic, retain) NSMutableArray *array;
    @property (nonatomic, retain) NSMutableDictionary *dictionary;
    @property (nonatomic, retain) NSMutableString *element;


    XMLParser.m
#import "xAppDelegate.h"
#import "XMLParser.h"

    @synthesize array, dictionary, element;
- (XMLParser *) initXMLParser { 
[super init];
appDelegate = (xAppDelegate *)[[UIApplication sharedApplication] delegate];

return self;    
}

- (void)parserDidStartDocument:(NSXMLParser *)parser {  
    array = [[NSMutableArray alloc] init];
        dictionary = [[NSMutableDictionary alloc] init];
}

    - (void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName 
    attributes:(NSDictionary *)attributeDict
{
    ele = [elementName copy];
    if ([elementName isEqualToString:@"CONTAINER"]) {
        element = [[NSMutableString alloc] init];
    }
}
- (void)parser:(NSXMLParser *)parser
foundCharacters:(NSMutableString *)string
{   
    if ([ele isEqualToString:@"ELEMENTNAME"]) {
        [element appendString:string];
    }
}
- (void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"CONTAINER"]) {
        [dictionary setObject:element forKey:@"ELEMENTNAME"];
        [array addObject:[dictionary copy]];
        }
}
- (void) dealloc {  
[element release];
[dictionary release];
[array release];
}

在我的控制器文件中,我这样做:

controller.h
@class XMLParser;

@interface controller : UIViewController {
 XMLParser *aXMLParser;
}

@property (nonatomic, retain) XMLParser *aXMLParser;

controller.m
#import "XMLParser.h"

@synthesize aXMLParser;

- (void)viewDidLoad {
    NSLog(@"test array: %@", aXMLParser.array);
    NSLog(@"test dict: %@", aXMLParser.dictionary);
    NSLog(@"test element: %@", aXMLParser.element);
}

当我测试 XMLParser.h 文件中的数组、字典或元素的值时,我得到结果。我做错了什么,所以我无法在控制器文件中调用我的结果? 欢迎任何帮助,因为我现在很困难:/

I use an instance of NSXMLParser. I store found chars in NSMutableStrings that are stored in an NSMutableDictionary and these Dicts are then added to an NSMutableArray.

When I test this everything seems normal: I count 1 array, x dictionnaries and x strings.

In a detailview controller file I want to show my parsed results. I call the class where everthing is stored but I get (null) returned.

This is what I do (wrong):

xAppDelegate.h

@interface xAppDelegate : NSObject <UIApplicationDelegate> {    
    UIWindow *window;
    UINavigationController *navigationController;
}

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

@end

xAppDelegate.m

#import "xAppDelegate.h"
#import "RootViewController.h"
#import "XMLParser.h"

@implementation xAppDelegate
@synthesize window, navigationController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // OFFLINE DOCUMENT > Resources folder
    NSString *Path = [[NSBundle mainBundle] bundlePath];
    NSString *DataPath = [Path stringByAppendingPathComponent:@"file.xml"];
    NSData *Data = [[NSData alloc] initWithContentsOfFile:DataPath];

    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:Data];       

    XMLParser *parser = [[XMLParser alloc] initXMLParser];

    [xmlParser setDelegate:parser]; 
    [xmlParser setShouldProcessNamespaces:NO];
    [xmlParser setShouldReportNamespacePrefixes:NO];
    [xmlParser setShouldResolveExternalEntities:NO];

    [xmlParser parse];      

    [window addSubview: navigationController.view];
    [window makeKeyAndVisible];
}

- (void)dealloc {
    [window release];
    [navigationController release];
    [super dealloc];
}
@end

    XMLParser.h
@class xAppDelegate;

    @interface XMLParser : NSObject {
        NSMutableArray *array;
        NSMUtableDictionary *dictionary;
        NSSMutabletring *element;
xAppDelegate *appDelegate;
    }
- (XMLParser *) initXMLParser;
    @property (nonatomic, retain) NSMutableArray *array;
    @property (nonatomic, retain) NSMutableDictionary *dictionary;
    @property (nonatomic, retain) NSMutableString *element;


    XMLParser.m
#import "xAppDelegate.h"
#import "XMLParser.h"

    @synthesize array, dictionary, element;
- (XMLParser *) initXMLParser { 
[super init];
appDelegate = (xAppDelegate *)[[UIApplication sharedApplication] delegate];

return self;    
}

- (void)parserDidStartDocument:(NSXMLParser *)parser {  
    array = [[NSMutableArray alloc] init];
        dictionary = [[NSMutableDictionary alloc] init];
}

    - (void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName 
    attributes:(NSDictionary *)attributeDict
{
    ele = [elementName copy];
    if ([elementName isEqualToString:@"CONTAINER"]) {
        element = [[NSMutableString alloc] init];
    }
}
- (void)parser:(NSXMLParser *)parser
foundCharacters:(NSMutableString *)string
{   
    if ([ele isEqualToString:@"ELEMENTNAME"]) {
        [element appendString:string];
    }
}
- (void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"CONTAINER"]) {
        [dictionary setObject:element forKey:@"ELEMENTNAME"];
        [array addObject:[dictionary copy]];
        }
}
- (void) dealloc {  
[element release];
[dictionary release];
[array release];
}

In my controller file I do this:

controller.h
@class XMLParser;

@interface controller : UIViewController {
 XMLParser *aXMLParser;
}

@property (nonatomic, retain) XMLParser *aXMLParser;

controller.m
#import "XMLParser.h"

@synthesize aXMLParser;

- (void)viewDidLoad {
    NSLog(@"test array: %@", aXMLParser.array);
    NSLog(@"test dict: %@", aXMLParser.dictionary);
    NSLog(@"test element: %@", aXMLParser.element);
}

When I test the value of my array, a dict or an element in the XMLParser.h file I get my result. What am I doing wrong so I can't call my results in my controller file?
Any help is welcome, because I'm pretty stuck right now :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身狗的梦 2024-09-12 14:36:51

您可能尚未初始化 NSMutableString 元素,因此您可能正在向 nil 对象发送消息。

这是一个猜测,因为您还没有发布重要的代码。我们需要看看

  • 你在哪里初始化了三个对象,
  • 并在哪里分配了它们,例如你可能在没有意识到的情况下将 nil 分配给了其中一个对象。

根据最新的代码进行编辑

代码有很多错误,例如,它泄漏了很多对象,但我看不到任何会导致您的特定问题的内容,除非 ELEMENTNAME元素不会出现在 XML 中的 CONTAINER 元素内。

我不是 iPhone 用户界面代码方面的专家,但您确定 applicationDidFinishLaunching 在 vi​​ewDidLoad 之前运行吗?

另外,您似乎从未将解析器分配给 aXMLParser 属性。

其他一些问题

你的 NSXMLParser 泄漏是因为你没有释放它。

您自己的解析器也会由于同样的原因而泄漏(您可能只想使用 XMLParser,而不是本地定义的解析器)。

您的 initXMPLarser 方法应该如下所示:

- (XMLParser *) initXMLParser { 
    self = [super init];
    if (self != nil)
    {
        appDelegate = (xAppDelegate *)[[UIApplication sharedApplication] delegate];
    }

    return self;    
}

即不要丢弃 [super init] 的结果并确保它不为零。

每次点击新元素时都会分配数组。每次遇到新元素时,都会丢弃并泄漏最后一个元素中的前一个数组。

每次点击 CONTAINER 元素时都会分配字典。您至少应该在分配新的之前释放旧的。

元件也会泄漏。

您放入数组中的字典副本也会泄漏。 Objective-C 集合保留其元素。

You probably haven't initialised the NSMutableString element, so you are probably sending messages to a nil object.

That's a guess because you haven't posted the important code. We need to see

  • where you initialise the three objects
  • where you assign them e.g. you might be assigning nil to one of them without realising it.

Edited following latest bit of code

There's quite a lot wrong with the code, for instance, it leaks a lot of objects, but I can't see anything that would cause your specific issue, unless the the ELEMENTNAME elements don't appear inside the CONTAINER elements in your XML.

I'm not an expert on user interface code on iPhone, but are you sure applicationDidFinishLaunching runs before viewDidLoad?

Also, you don't seem to ever assign your parser to the aXMLParser property.

Some of the Other Issues

Your NSXMLParser leaks because you don't release it.

Your own parser also leaks for the same reason (you probably just want to use aXMLParser, not a locally defined one).

Your initXMPLarser method should look like:

- (XMLParser *) initXMLParser { 
    self = [super init];
    if (self != nil)
    {
        appDelegate = (xAppDelegate *)[[UIApplication sharedApplication] delegate];
    }

    return self;    
}

i.e. don't throw away the result of [super init] and make sure it is not nil.

array is allocated every time you hit a new element. Each tilme you hit a new element, you throw away and leak the previous array from the last element.

dictionary is allocated every time you hit a CONTAINER element. You should at least release the old one before allocating the new one.

element also leaks.

The copy of dictionary you put into the array also leaks. Objective-C collections retain their elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文