Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSDictionary的NSArray-当数组中只有一个字典,而不是一个对象的数组时返回NSDictionary_Ios_Objective C_Arrays_Dictionary - Fatal编程技术网

Ios NSDictionary的NSArray-当数组中只有一个字典,而不是一个对象的数组时返回NSDictionary

Ios NSDictionary的NSArray-当数组中只有一个字典,而不是一个对象的数组时返回NSDictionary,ios,objective-c,arrays,dictionary,Ios,Objective C,Arrays,Dictionary,我对Objective C比较陌生。我有一个包含NSDictionary对象的NSArray。我正在调用web服务来获取数据。我有一个PickerView,这是它的数据源(我实现了numberOfRows): 这是我的“proba”,数组中有3个NSDictionary: 这是我的“proba”,数组中只有一个NSDictionary 问题是,当我的服务返回1个字典时,数组中需要有1个NSDictionoray对象。如何解决这个问题?谢谢 编辑: 我的服务数组,这是我的选择器 - (void

我对Objective C比较陌生。我有一个包含NSDictionary对象的NSArray。我正在调用web服务来获取数据。我有一个PickerView,这是它的数据源(我实现了numberOfRows):

这是我的“proba”,数组中有3个NSDictionary:

这是我的“proba”,数组中只有一个NSDictionary

问题是,当我的服务返回1个字典时,数组中需要有1个NSDictionoray对象。如何解决这个问题?谢谢

编辑: 我的服务数组,这是我的选择器

- (void)SOAPManager:(SOAPManager *)client didSuccesWithoutError:(id)responseObject
{
SearchResult *result = [SearchResult new];
result.myArray = [responseObject allValues];
self.Result = result;
NSLog(@"Loaded successfully: %@", result.retailers);

}
“self.Result”是通过RACObserver观察到的,因此我可以将其用于我的ViewModel

这是结果。零售商有3本字典:

这是一本字典:


是的,当只有一个对象时,它只返回Dictionary

你的问题不清楚,但是我猜你有一个web服务,如果有多个结果,它会返回一个数组,如果只有一个结果,它只返回结果本身,在后一种情况下,你需要的是一个结果数组

您可以通过使用
isKindOfClass:
检查web服务返回的值的类来解决这个问题。概括地说,这可能是:

id myWebServiceResult = ...;

if (![myWebServiceResult isKindOfClass:[NSArray class]]) // note the ! - not
{
   // the result is not an array, make it an array of one item
   myWebServiceResult = @[ myWebServiceResult ];
}

很遗憾,你的问题一点也不清楚。选择器代码似乎与您的
proba
数据结构没有任何关系。您是否正在尝试将服务器响应解析为不同的内容?如果是,请提供该代码(用于构建该数组的代码)。如果
proba
声明为NSString,为什么要将其显示为数组或字典?只是为了在调试器中看到,我再次使用了NSLog,如果
proba
声明为NSString,为什么调试器会将其显示为数组或字典?是的,我从一开始就怀疑这一点,这很可能是正确的答案。然而,如果可能的话,真正正确的答案是实际更改API。
id myWebServiceResult = ...;

if (![myWebServiceResult isKindOfClass:[NSArray class]]) // note the ! - not
{
   // the result is not an array, make it an array of one item
   myWebServiceResult = @[ myWebServiceResult ];
}