NSInvalidArgumentException',原因:' - [__ NSPlaceholderDictionary initWithObjects:forKeys:error

时间:2011-01-17 09:10:41

标签: iphone

我使用NSMutableUrl请求从.net网络服务器获取网络数据。

我通过使用NSXmlparsing解析响应来获取我在NSDirectory中存储后使用soap请求获取数据。

我的代码是

NSArray *keys = [NSArray arrayWithObjects:@"id", @"firstname", @"lastname",@"addr",@"state",@"country",@"email",@"phone", nil];
    directory = [NSDictionary dictionaryWithObjects:resultData forKeys:keys];

 [[NSUserDefaults standardUserDefaults] setObject:directory forKey:@"dictionaryKey"];

它工作正常,但如果我输错了,就会出现这样的异常。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSPlaceholderDictionary initWithObjects:forKeys:]: number of objects (0) not equal to number of keys (8)'

我正在收到像这样的网络服务器的响应

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GenericAndroidMethodResponse xmlns="Mortgage"><GenericAndroidMethodResult>&lt;NewDataSet /&gt;</GenericAndroidMethodResult></GenericAndroidMethodResponse></soap:Body></soap:Envelope>

申请退出。

但是,如果我得到错误的回答我怎么能识别,那时我需要显示警报但不要退出应用程序。

我该怎么做呢。

我认为我的问题很明确。

任何人都可以帮助我。

提前感谢你。

2 个答案:

答案 0 :(得分:3)

您正在尝试创建一个字典并将其传递给8个密钥(您的keys变量),但是您没有传递相同数量的值。事实上,Cocoa说你的resultData是一个空数组(或者甚至是零)。因此,您需要使用以下内容保护您的代码:

if ([resultData count] == 8) {
   // Creating the dictionary will succeed.
   directory = ... ;
} else {
   // Creating the dictionary will fail, handle or ignore that.
}

答案 1 :(得分:0)

假设resultDataNSArray,您可以测试[resultData count] == 8