Iphone NSInvalidArgumentException';,原因:'-[\uu NSPlaceholderDictionary initWithObjects:forKeys:错误
我正在使用NSMutableUrl请求从.net web服务器获取web数据 在获取数据之后,我使用soap请求获取数据,并通过使用NSXmlparsing解析响应将数据存储在NSDirectory中 我的代码是Iphone NSInvalidArgumentException';,原因:'-[\uu NSPlaceholderDictionary initWithObjects:forKeys:错误,iphone,Iphone,我正在使用NSMutableUrl请求从.net web服务器获取web数据 在获取数据之后,我使用soap请求获取数据,并通过使用NSXmlparsing解析响应将数据存储在NSDirectory中 我的代码是 NSArray *keys = [NSArray arrayWithObjects:@"id", @"firstname", @"lastname",@"addr",@"state",@"country",@"email",@"phone", nil]; directory =
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)'
我从web服务器得到的响应如下
<?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><NewDataSet /></GenericAndroidMethodResult></GenericAndroidMethodResponse></soap:Body></soap:Envelope>
NewDataSet/
然后应用程序退出
但若我得到了错误的响应,我如何才能识别,在那个时我需要显示一个警报,但不退出应用程序
我怎样才能做到这一点
我想我的问题很清楚
谁能帮我一下吗
提前感谢您。假设
resultData
是一个NSArray
,您可以测试[resultData count]==8
您正在尝试创建一个字典并向它传递8个键(您的键
变量),但您传递的值数不相等。事实上,Cocoa说您的resultData
是一个空数组(甚至可能是零)。因此您需要使用以下内容来保护代码:
if ([resultData count] == 8) {
// Creating the dictionary will succeed.
directory = ... ;
} else {
// Creating the dictionary will fail, handle or ignore that.
}