Can't Send NSMutableDictionary To Another Class

拟墨画扇 提交于 2020-02-24 14:10:28

问题


All,

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.

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.

EDIT: I do use the initXMLParser method to initialize in the other class. But even still, accessing within this class doesn't even work. In the getResponse method that displays the response dictionary, the NSLog doesn't display anything but "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

回答1:


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.

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




回答2:


A few things a little bit off here.

Are you using ARC? If not you will leak memory all over the place as you are not doing any memory management.

Your designated initializer - (id)initXMLParser; is wrong in a couple of places.

I can't actually see anywhere in your code that uses response.

To fix the your designated init method you should do this.

  1. Remove the unused init method
  2. The init method should return a type of id
  3. You should ensure you override the method correctly like so

    - (id)initXMLParser
    {
        self = [super init];
        if (self) {
          response = [[NSMutableDictionary alloc] init];
          count = 1;
        }
        return self;
    }
    

This will make sure your init method is correct for a start but you still have the issue that no data is actually being added to the response at any point.



来源:https://stackoverflow.com/questions/6599955/cant-send-nsmutabledictionary-to-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!