无法将 NSMutableDictionary 发送到另一个类
所有,
我试图将 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定使用 initXMLParser 方法来初始化实例吗?您可能正在使用常规的init,并且那里没有初始化response变量。
总的来说,此类问题通常很容易追踪。如果某个东西是nil而不是一个实例——那么它就没有被初始化或者在某个地方被取消了。在您的代码中,有两行带有响应分配,并且都应该有效。所以,我猜它错过了初始化(并且带有第二个 init 的 if -> if 分支从未被调用过)。
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).
这里有一些事情有点不对劲。
你用的是ARC吗?如果没有,您将到处泄漏内存,因为您没有进行任何内存管理。
您指定的初始化程序
- (id)initXMLParser;
在几个地方是错误的。我实际上看不到代码中使用
response
的任何地方。要修复您指定的 init 方法,您应该这样做。
id
类型您应该确保正确地重写该方法,如下所示
<前><代码>- (id)initXMLParser
{
self = [超级初始化];
如果(自我){
响应 = [[NSMutableDictionary alloc] init];
计数 = 1;
}
返回自我;
}
这将确保您的 init 方法一开始是正确的,但您仍然遇到没有数据实际添加到响应中的问题/code> 在任何时候。
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.
id
You should ensure you override the method correctly like so
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.