我将mycode发布到json但我的应用程序错误
“因未捕获的异常而终止应用 'NSInvalidArgumentException',原因:'+ [NSDictionary dictionaryWithObjectsAndKeys:]:每对的第二个对象必须是 非空。或者,您是否忘记了终止参数列表?'“
-(void)requesdatesendorder:(NSArray *)foodid{
NSString *FoodId ;
for (int i =0; i < foodid.count; i++) {
NSString *idfood = [foodid objectAtIndex:i];
if (FoodId.length == 0) {
FoodId = [NSString stringWithFormat:@"%@,food_id[%d]",idfood,i];
}
else{
FoodId = [NSString stringWithFormat:@"%@,%@,food_id[%d]",FoodId,idfood,i];
}
}
NSLog(@"%@",FoodId);
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"date_send_order",@"params",
FoodId
,nil];
[self requestWithMethod:@"POST"
urlString:kFeedBeeservice
parameters:params
mode:Beeservicedatesendorder];
}
但我想发送json格式 http://i.stack.imgur.com/tkfqv.png
答案 0 :(得分:3)
您尚未为 FoodId 指定任何键。您的NSDictionary
语法错误。它应该如下,
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"Object1",@"key1",@"object2",@"key2",nil];
在您的代码中为FoodId添加一个键或为FoodId添加一个对象。它将解决您的错误。
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"date_send_order",@"params",
FoodId,@"key",nil];
OR
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"date_send_order",@"params",@"object"
FoodId,nil];
答案 1 :(得分:0)
我看到有三个问题:
一旦你有了字符串,就打破for
循环:
NSString *foodIdStr = nil;
for (int i =0; i < foodid.count && foodIdStr == nil; i++) {
NSString *idfood = [foodid objectAtIndex:i];
if (FoodId.length == 0) {
foodIdStr = [NSString stringWithFormat:@"%@,food_id[%d]",idfood,i];
}
else{
foodIdStr = [NSString stringWithFormat:@"%@,%@,food_id[%d]",FoodId,idfood,i];
}
}
确保您没有为字典分配nil
值:
if (foodIdStr == nil) {
NSLog(@"Failed to get food id");
return;
}
您使用了错误数量的参数dictionaryWithObjectsAndKeys
:
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"date_send_order", @"params",
foodIdStr, @"food_id", // Here
nil];