我有一个使用 RestKit 和 CoreData 的 iOS 应用程序。我在发出 POST 请求时遇到了障碍,因为请求正文是由NSDictionary
. 我遇到的问题是请求正文需要有重复的键。 NSDictionary
需要唯一的键,所以我不知道如何使这项工作。
这是服务器期望请求正文的方式。
<node>
<personId>2</personId>
<status>2</status>
<title>Dinosaur unearthed somewhere out there. </title>
<point>
<city>Somewhere</city>
<copy><![CDATA[An amazing discovery was unearthed as a local farmer was plowing his field]]></copy>
<state>Outthere</state>
<sequence>1</sequence>
</point>
<point>
<city>Somwhere</city>
<copy><![CDATA[Archeologists from around the world are amazed at the pristine condition of the remains. Also of note is that the farmer was only using a single blade plow on his John Deere tractor when it was unearthed. ]]></copy>
<sequence>2</sequence>
<state>Outthere</state>
</point>
<point>
......
</point>
</node>
这是我尝试使其工作的简化版本....
params = [[NSMutableDictionary alloc] init];
nodes = [[NSMutableDictionary alloc] init];
nodeParams = [[NSMutableDictionary alloc] init];
NSString *personId = @"2";
NSString *status = @"2";
NSString *title = @"Dinosaur unearthed somewhere out there.";
NSString *city = @"Somewhere";
NSString *state = @"OutThere";
NSString *copyField = @"Testing this out to see if it works";
//Here I set up the point layer of the request body
//In my code this three line section is in a loop. Obviously this does not work because it just overwrites the objectForKey each time through the loop.
[params setObject:copyField forKey:@"copy"];
[params setObject:city forKey:@"city"];
[params setObject:state forKey:@"state"];
//Here I Set up the Node Layer of the request body
[nodeParams setObject:params forKey:@"point"];
[nodeParams setObject:personId forKey:@"personId"];
[nodeParams setObject:status forKey:@"status"];
[nodeParams setObject:title forKey:@"title"];
[nodes setObject:nodeParams forKey:@"node"];
NSLog(@"The Dictionary is %@",nodes);
在运行时,日志显示主体的格式正确,除了它们只是一个点层,并且它填充了循环中最后一次传递的数据。有谁知道解决这个问题的任何技巧?
作为注释,我认为该postObject
方法需要 NSDictionary,因为它将字典传递给 JSON 序列化工具。我怀疑序列化工具期望传入字典。如果有人知道,如果我错了,请纠正我。