2012-07-24 98 views
0

我想从我的NSDictionary检索值,但是我遇到了错误,我想知道如果有人能够帮助我解决我的问题。从NSDictionary返回值

我有18个值,并不是所有的显示在这里,我正在检查,当正确的键已达到我想在NSDictionary中的值,并将其传递到我的NSString变量之一。

下面是什么我想就像我说的,我有几个问题

for (id key in searchData) { 
    if ([[searchData objectForKey:key] isEqualToString:@"Code"]) { 
     codeSeries = [searchData objectForKey:key]; 
    } 
    else if ([[searchData objectForKey:key] isEqualToString:@"ID"]) { 
     IDSeries = [searchData objectForKey:key]; 
    } 

    // ... 

但是,当我尝试登录的任何值,以做不过的例子,他们都返回Null。我已经在手边检查过字典,并且所有值都绝对存在,所以我认为上面的代码有问题。

任何帮助将不胜感激。

更新

这是我创建的NSDictionary

//start of mymethod... 

NSDictionary *sendSeriesDictionary = [[NSDictionary alloc] init]; 

    // Keys for sendSeriesDictionary 
    NSArray *keys = [NSArray arrayWithObjects:@"Code", @"ID", nil]; 

    // Objects for keys that are for sendSeriesDictionary 
    NSArray *objects = [NSArray arrayWithObjects: [NSNull null], IdString, nil]; 

    // Add keys and objects to NSDictionary 
    sendSeriesDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 


[engineRequests SeriesSearch:sendSeriesDictionary]; // this is where I send the NSDictionary over to where I want to read it. 

//end of mymethod 

回答

1

你有几个问题。首先,你混合键和值(如汤姆说)。其次,在创建字典时或者至少是不必要的实例化时,您可能会遇到内存泄漏。

尝试此创建词典:

// Keys for sendSeriesDictionary 
NSArray *keys = [NSArray arrayWithObjects:@"Code", @"ID", nil]; 

// Objects for keys that are for sendSeriesDictionary 
NSArray *objects = [NSArray arrayWithObjects: [NSNull null], IdString, nil]; 

// Add keys and objects to NSDictionary 
NSDictionary *sendSeriesDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 

检索值可以这样做:

codeSeries = [searchData objectForKey:@"Code"]; 
IDSeries = [searchData objectForKey:@"ID"]; 

在你的第一个循环您完成所有的键循环,得到了他们的值,然后再比较那些关键。这是没有意义的。

+0

完美!!!!! :)非常感谢..是的,我不知道为什么我没有没有像原来的NSDictionary一样删除...我认为我做了两部分代码,并没有轮到清理它。 – HurkNburkS 2012-07-25 01:14:45

1

你是混合键和值?

也许你只是想codeSeries = [searchData objectForKey:@"Code"];

+0

好的我会尝试这个..我在字典上迷失了..我知道如何创建它们,但不确定如何很好地使用它们。 – HurkNburkS 2012-07-24 21:20:19

+0

这也是我的第一个想法。并且'if([(NSString *)key isEqualToString:@“Code”])'另外,为了更容易...您可以使用'for(NSString * key in searchData)'然后使用'if([key isEqualToString :@“Code”])' – 2012-07-24 21:20:42

+0

也许你应该展示你如何创建字典或发送当前字典内容的日志输出 – Tom 2012-07-24 21:31:28