无法将NSMutableDictionary发送到另一个类 [英] Can't Send NSMutableDictionary To Another Class

查看:85
本文介绍了无法将NSMutableDictionary发送到另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

我正在尝试将NSMutableDictionary响应发送给我的另一类,或者让另一类从该类中提取字典。当另一个类使用 getResponse方法时,它返回null。

I'm trying to send the NSMutableDictionary "response" to another class of mine, or rather, have the other class pull the dictionary from this class. When the other class uses the "getResponse" method, it returns null.

我附带的代码是我的XML解析器,这就是将我需要的信息放入字典中的原因。最底部是在NSLog中显示(null)的方法。我已为您发表评论。

The code I have attached is my XML parser, which is what puts the information I need into the dictionary. At the very bottom is the method that shows "(null)" in the NSLog. I have commented it for you.

编辑:我确实使用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 ,并且没有初始化

Are you sure you use initXMLParser method to initialize the instance? You might be using regular init and there is no initialization of the response variable there.

总的来说,此类问题通常很容易跟踪。如果某些内容是 nil 而不是实例-从未进行过初始化或在某处无效。在您的代码中,有两行带有响应分配,并且两行都应正常工作。因此,我猜想它缺少初始化(以及带有第二个init避风港的if-> if分支)。

Overall, such issues are usually easy to track. If something is nil instead of an instance - than it wasn't ever initialized or was somewhere nullified. In your code, there are two lines with response assignment and both should work. So, my guess it's missed initialization (and the if -> if branch with the second init haven't been ever called).

这篇关于无法将NSMutableDictionary发送到另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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