繁体   English   中英

无法将 NSMutableDictionary 发送到另一个 Class

[英]Can't Send NSMutableDictionary To Another Class

全部,

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

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

编辑:我确实使用 initXMLParser 方法在其他 class 中进行初始化。 但即便如此,在这个 class 中访问甚至都不起作用。 在显示响应字典的 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并且那里没有响应变量的初始化。

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

这里有些东西有点不对劲。

你在用ARC吗? 如果没有,您将到处泄漏 memory,因为您没有进行任何 memory 管理。

您指定的初始化程序- (id)initXMLParser; 在几个地方是错误的。

我实际上在您的代码中看不到任何使用response的地方。

要修复您指定的 init 方法,您应该这样做。

  1. 删除未使用的 init 方法
  2. init 方法应该返回一个类型的id
  3. 您应该确保像这样正确覆盖该方法

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

这将确保您的 init 方法在开始时是正确的,但您仍然会遇到在任何时候实际上都没有将数据添加到response中的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM