目标C:将NSDictionary从一个类传递到另一个类

时间:2011-07-07 19:49:29

标签: iphone objective-c xcode

全部,

免责声明:我知道还有其他问题可以提出类似的问题,但没有一个提供我理解的答案或适用于我的情况的答案。

我有两个类,classA和classB。在classA中有一个void实例方法,它创建一个NSMutableDictionary(如果你想要特定的,它是一个XMLparser)。在运行classA的XMLParser并创建并填充NSMutableDictionary之后,调用classB,需要对该字典执行其他一些操作。由于某种原因,classB无法访问classA中的NSMutableDictionary(实际上,它可以访问它,但由于某种原因它显示为“NULL”)。我该怎么办?

提前致谢。

编辑:你问了源代码,你知道了。下面的ClassA,有问题的字典称为“响应”。

#import "XMLParser.h"
#import "CardSetupViewController.h"

@implementation XMLParser

@synthesize response;

- (XMLParser *) initXMLParser
{
    self = [super init];
    // init dictionary of response data 
    response = [[NSMutableDictionary alloc] init];
    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

        //THIS SHOULD NEVER NEED TO BE USED
            if (!response)//if array is empty, it makes it!
            {
                NSLog(@"Dictionary is empty for some reason, creating...");
            response = [[NSMutableDictionary alloc] init];
            }
        //END: THIS SHOULD NEVER BE USED
        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 {
        [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;
}


@end

4 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

currentElementName还是currentElementValue ivars?如果是这样,每次你捕获一个元素或一个值,你必须释放你的ivar并保留你捕获的新值。

另外,正如评论所说,你没有遵循关于编写正确的初始化程序的任何规则。

classB怎么样?是不是有一些逻辑会让你的词典被释放? 尝试管理更多的内存。

答案 2 :(得分:0)

你有几个选择......实际上有很多选择

选项A :(设置)

@interface ClassB{
NSDictionary * someDict;
}
-(void)setSomeDict:(NSDictionary *)aDict;
@end

@implementation ClassB
-(void)setSomeDict:(NSDictionary *)aDict
{
   someDict = [aDict retain];//or copy depending on your needs;
}

@end

somewhere in ClassA...

ClassB * b = [ClassB new];
[b setSomeDict: someOtherDict];

选项B :(推送)

@interface ClassB{
NSDictionary * someDict;
}
-(void)doSomethingWithDict:(NSDictionary *)aDict;
@end

@implementation ClassB
-(void)doSomethingWithDict:(NSDictionary *)aDict
{
   NSLog(@"did something with aDict: %@",aDict);
}

@end

somewhere in ClassA...

ClassB * b = [ClassB new];
[b doSomethingWithDict: someOtherDict];

选项c:(init)

@interface ClassB{
NSDictionary * someDict;
}
-(id)initWithDict:(NSDictionary *)aDict;
@end

@implementation ClassB
-(id)initWithDict:(NSDictionary *)aDict
{
   self = [super init];
   if(self)
   {
        someDict = [someDict retain]; //or copy depending on your needs
   }
   NSLog(@"did something with aDict: %@",aDict);
}

@end

somewhere in ClassA...

ClassB * b = [[ClassB alloc] initWithDict:someOtherDict];
[b doSomethingelse];

你也可以使用属性等...有很多选项,但是你也应该理解对象的所有权,这样你就不会泄漏字典了。

-(void)dealloc
{
    [someDict release];
}

应该添加到classB。

答案 3 :(得分:0)

首先检查你是否在NSXMLParser的委托方法中获得了响应

- (void)parserDidEndDocument:(NSXMLParser *)parser;

并通过委托发送响应(你应该在这个类中实现),委托方法是在另一个类中实现的......简单!!!