iphone - 无法将 NSMutableDictionary 发送到另一个类

标签 iphone objective-c cocoa nsmutabledictionary

全部,

我正在尝试将 NSMutableDictionary“响应”发送到我的另一个类,或者更确切地说,让另一个类从这个类中提取字典。当另一个类使用“getResponse”方法时,它返回 null。

我附加的代码是我的 XML 解析器,它将我需要的信息放入字典中。最底部是在 NSLog 中显示“(null)”的方法。我已经给你评论了。

编辑:我确实使用 initXMLParser 方法在另一个类中进行初始化。但即便如此,在这个类中访问仍然不起作用。在显示响应字典的 getResponse 方法中,NSLog 除了“TEST(null)”之外不显示任何内容。

#import "XMLParser.h"

@implementation XMLParser

@synthesize response;


- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (XMLParser *) initXMLParser //not sure if this is necessary anymore - CHECK THIS
{
    self = [super init];
    // init array of return data 
    NSLog(@"Initializing self and super...");
    response = [[NSMutableDictionary alloc] init];
    count = 1;
    return self;
}

//Gets Start Element of SessionData
- (void)parser:(NSXMLParser *)parser 
        didStartElement:(NSString *)elementName 
        namespaceURI:(NSString *)namespaceURI 
        qualifiedName:(NSString *)qualifiedName 
        attributes:(NSDictionary *)attributeDict 
{

    if ([elementName isEqualToString:@"SessionData"]) 
    {
        NSLog(@"Found SessionData in the return XML! Continuing...");
        //response is a NSMutableArray instance variable (see .h of this)
        if (!response)//if array is empty, it makes it!
        {
        NSLog(@"Dictionary is empty for some reason, creating...");
            response = [[NSMutableDictionary alloc] init];
            count=1;
        }
        return;
    }
    else
    {
        currentElementName = elementName;
        NSLog(@"Current Element Name = %@", currentElementName);
        return;
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{
    if (!currentElementValue) {
        // init the ad hoc string with the value     
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    } else {
        // append value to the ad hoc string    
        [currentElementValue setString:string];
        NSLog(@"Processing value for : %@", string);
    }
}  

//Gets End Element of SessionData
- (void)parser:(NSXMLParser *)parser 
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"SessionData"]) 
    {
        // We reached the end of the XML document
        //dumps dictionary into log
        NSLog(@"Dump:%@", [response description]);
        return;
    }
    else
    {
        //Adds key and object to dictionary
        [response setObject:currentElementValue forKey:currentElementName];
        NSLog(@"Set values, going around again... brb.");
    }
    currentElementValue = nil;
    currentElementName = nil;
}

- (NSMutableDictionary*)getResponse
{
    NSLog(@"%@", [self response]; //THIS RETURNS NULL
    return response;
}


@end

最佳答案

您确定使用initXMLParser方法来初始化实例吗?您可能正在使用常规的 init,并且那里没有初始化 response 变量。

总体而言,此类问题通常很容易跟踪。如果某个东西是nil而不是一个实例——那么它就没有被初始化或者在某个地方被取消了。在您的代码中,有两行带有响应分配,并且都应该有效。所以,我猜它错过了初始化(并且带有第二个 init 的 if -> if 分支从未被调用过)。

关于iphone - 无法将 NSMutableDictionary 发送到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6599955/

相关文章:

objective-c - 从KVO中检索 `change`字典中的信息

iphone - 适用于 iPhone 应用程序的 Google Doc Api

cocoa - 通过标签号获取 NSTextField ?

iphone - 在 iPhone 应用程序中嵌入 youtube 视频

objective-c - copyWithZone 错误

ios - 循环和编辑 UITextField 占位符不起作用

javascript - PhoneGap/Cordova 将 appdelegate 变量传递给 JS

macos - Mac OS X 应用程序可以阻止 dlopen 加载库吗?

iphone - MKMapView 更改 View 中的物理位置

iphone - 如何通过网络链接在 Google map iPhone 应用程序上放置图钉?