Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.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/25.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:';NSInvalidArgumentException';,原因:'-[\uu NSCFString SorterDarrayUsingFunction:context:]:发送到实例的选择器无法识别。_Ios_Objective C_Iphone - Fatal编程技术网

iOS:';NSInvalidArgumentException';,原因:'-[\uu NSCFString SorterDarrayUsingFunction:context:]:发送到实例的选择器无法识别。

iOS:';NSInvalidArgumentException';,原因:'-[\uu NSCFString SorterDarrayUsingFunction:context:]:发送到实例的选择器无法识别。,ios,objective-c,iphone,Ios,Objective C,Iphone,我在尝试获取前10组钥匙时遇到以下崩溃。下面是导致问题的代码 for (int i = 0; i < 10; i++) { sortedArray = [ [answers allKeys][i] sortedArrayUsingFunction:sort context:nil]; } for(int i=0;i

我在尝试获取前10组钥匙时遇到以下崩溃。下面是导致问题的代码

  for (int i = 0; i < 10; i++) {

        sortedArray = [ [answers allKeys][i] sortedArrayUsingFunction:sort context:nil];

        }
for(int i=0;i<10;i++){
SorterDarray=[[answers allKeys][i]SorterDarray使用函数:排序上下文:nil];
}

然而,当我只使用这个
sortedArray=[[answers allKeys]sortedArray usingfunction:sort context:nil]我没有崩溃。有人能帮我解决这个问题吗?我只想要前10把钥匙。

我只是猜一下,但我想你想要这个

NSArray *arr = [[answers allKeys] subarrayWithRange:NSMakeRange(0, 10)];
NSArray *sorted = [arr sortedArrayUsingFunction:sort context:nil];
不幸的是,这不起作用,您需要更改您的体系结构。原因如下

字典是无序的。这意味着,
所有键
可以在任何时候以任何顺序返回。特别是在迭代的地方,不太可能,但有可能会有一个数组填充了所有相同的键。如果您使用我上面提供的代码,您将得到一个包含10个键的排序数组。虽然这些键是唯一的,但它们不会按任何顺序确定,应该假定为随机键

也许在筛选出数组之前需要对密钥进行排序?可能看起来像这样:

NSArray *sorted = [[answers allKeys] sortedArrayUsingFunction:sort context:nil];
NSArray *filtered = [sorted subarrayWithRange:NSMakeRange(0, 10)];
NSArray* sortedArray = [[answers allKeys] sortedArrayUsingFunction:sort context:nil];
NSArray* sorted1st10 = [sortedArray subarrayWithRange:NSMakeRange(0, 10)];

由于
[answers allkeys][i]
正在返回一个NSString,因此您将发生崩溃

也许你正在尝试做的事情可以这样完成:

NSArray *sorted = [[answers allKeys] sortedArrayUsingFunction:sort context:nil];
NSArray *filtered = [sorted subarrayWithRange:NSMakeRange(0, 10)];
NSArray* sortedArray = [[answers allKeys] sortedArrayUsingFunction:sort context:nil];
NSArray* sorted1st10 = [sortedArray subarrayWithRange:NSMakeRange(0, 10)];

似乎在某个地方,
[answers allkey][i]
是一个
NSString
,而不是一个
NSArray
。NSString不是NSArray。你认为它会如何工作?即使排序本身起作用,它也不能返回多个条目,因此存储在
sortedArray
中的指针将(最多)包含一个元素。循环的每次迭代都会用新值替换先前的
sortedArray
,这意味着只有最后一次迭代才有效果;可能是因为字典是无序的,所以排序是明确的。但是,不,问题中没有足够的信息让人有信心。