2011-01-17 10 views
2

NSMutableUrlリクエストを使用して.net WebサーバーからWebデータを取得しています。NSInvalidArgumentException '、[理由:' - [__ NSPlaceholderDictionary initWithObjects:forKeys:エラー

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> 

のように、Webサーバからの応答を取得していますし、アプリケーションが終了します。

しかし、私が間違った応答を得た場合、その時点で私は警告を表示する必要がありますが、アプリケーションから終了する必要はありません。

どうすればいいですか?

私の質問ははっきりしていると思います。

いずれかのplsが私を助けることができます。

ありがとうございます。

答えて

3

辞書を作成して8つのキー(変数keys)を渡そうとしていますが、同じ数の値は渡していません。実際には、ココアはあなたのresultDataが空の配列(または、おそらくゼロ)であると言います。だからあなたのコードを保護する必要があります:

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

resultDataNSArrayであると仮定すると、[resultData count] == 8であるかどうかをテストできます。

関連する問題