【问题标题】:Can't Send NSMutableDictionary To Another Class无法将 NSMutableDictionary 发送到另一个类
【发布时间】:2011-07-06 16:37:40
【问题描述】:

全部,

我正在尝试将 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

【问题讨论】:

  • 我没有看到返回response的方法? - (NSDictionary *)response在哪里?
  • 有了这个属性,他还需要一个吗?
  • !response不测试response指向的对象是否为空;它测试该变量是否指向任何对象。空字典与没有字典有很大不同;您正在测试后者。

标签: iphone objective-c cocoa nsmutabledictionary


【解决方案1】:

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

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

【讨论】:

  • 我确实使用 initXMLParser 方法在另一个类中进行初始化。但即便如此,在显示响应字典的 getResponse 方法中,NSLog 只显示“TEST(null)”。
  • @James Martinez:你没有响应字典。这就是“null”的意思:response 持有nil,而不是指向字典的指针。这意味着要么你用nil 替换了指向字典的指针,要么你从来没有把指向字典的指针放在变量中。就像 Gobra 所说的那样,您在两个地方都指定了 response,并且都始终将其设置为指向字典的指针,因此剩下的唯一结论是您还没有到达其中任何一个地方。
  • @James Martinez:所以我们知道您没有向该对象发送initXMLParser 消息。如果你是,它会有一个响应字典。您大概正在发送它init,您已经实现了它而没有任何其他行为。我建议将 init 的空实现替换为 initXMLParser 的实现,并删除不必要的单独 initXMLParser 方法。
  • 我正在向这个对象发送 initXMLParser。我已经在当前版本中注释掉了init。
  • @James Martinez:注释掉init 的实现并不意味着你没有发送它。 init 由 NSObject 实现;这就是[super init] 的工作方式。如果您真的没有发送init,另一种可能性是您在笔尖中创建了此类的另一个实例;该对象与您通过发送allocinitXMLParser 消息创建的对象无关。您已经创建了两个这样的对象,并且您正试图从 nib 中的一个中获取响应字典,该字典没有一个。删除您在 nib 中创建的对象。
【解决方案2】:

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

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

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

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

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

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

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

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

【讨论】:

  • 我确实使用 initXMLParser 方法在另一个类中进行初始化。但即便如此,在这个类中访问甚至都行不通。在显示响应字典的 getResponse 方法中,NSLog 只显示“TEST(null)”。
  • 它真的应该只是init。没有理由让简单的旧 init 里面什么都没有。
猜你喜欢
  • 1970-01-01
  • 2022-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多