Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.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
Iphone 目标C中的查询数组(如MySQL)_Iphone_Objective C_Ios_Filter - Fatal编程技术网

Iphone 目标C中的查询数组(如MySQL)

Iphone 目标C中的查询数组(如MySQL),iphone,objective-c,ios,filter,Iphone,Objective C,Ios,Filter,所以基本上我有一个巨大的数组(只有一个二维数组) 我的根数组有100个子数组 我想查询根/子数组并只返回其2对象等于hello的子数组 所以基本上我在下面有一个虚构的想法 updatedArray = [rootArray WHERE childArray objectAtIndex:2 == @"hello"]; 现在,正如您所看到的,我希望更新后的数组包含根数组中的40或50个子数组 明白我的意思了吗?它有点像MySQL,只有一个数组而不是一个数据库?试试这个: NSMutableArra

所以基本上我有一个巨大的数组(只有一个二维数组)

我的根数组有100个子数组

我想查询根/子数组并只返回其2对象等于hello的子数组

所以基本上我在下面有一个虚构的想法

updatedArray = [rootArray WHERE childArray objectAtIndex:2 == @"hello"];
现在,正如您所看到的,我希望更新后的数组包含根数组中的40或50个子数组

明白我的意思了吗?它有点像MySQL,只有一个数组而不是一个数据库?

试试这个:

NSMutableArray *updated = [[NSMutableArray alloc] init];
for (NSArray *a in rootArray)
{
    if ([[a objectAtIndex:2] isEqualToString:@"hello"])
        [updated addObject:a];
}
现在,
updated
将包含
rootArray
中的数组,其第三个对象是
@“hello”

使用后不要忘记释放(如果不使用ARC)

您还可以使用谓词进行简单逻辑;请参见

尝试以下操作:

NSMutableArray *updated = [[NSMutableArray alloc] init];
for (NSArray *a in rootArray)
{
    if ([[a objectAtIndex:2] isEqualToString:@"hello"])
        [updated addObject:a];
}
现在,
updated
将包含
rootArray
中的数组,其第三个对象是
@“hello”

使用后不要忘记释放(如果不使用ARC)


您还可以使用谓词进行简单逻辑;请参见

您可以使用
NSPredicate
过滤阵列,如下所示:

NSArray *data = [NSArray arrayWithObjects:
    [NSArray arrayWithObjects:@"One", @"Two", nil]
,   [NSArray arrayWithObjects:@"Three", @"Four", nil]
,   [NSArray arrayWithObjects:@"Nine", @"Two", nil]
,   nil];
NSPredicate *filter = [NSPredicate predicateWithBlock:^BOOL(id array, NSDictionary *bindings) {
    // This is the place where the condition is specified.
    // You can perform arbitrary testing on your nested arrays
    // to determine their eligibility:
    return [[array objectAtIndex:1] isEqual:@"Two"];
}];
NSArray *res = [data filteredArrayUsingPredicate:filter];
NSLog(@"%lu", res.count); 

您可以使用
NSPredicate
过滤阵列,如下所示:

NSArray *data = [NSArray arrayWithObjects:
    [NSArray arrayWithObjects:@"One", @"Two", nil]
,   [NSArray arrayWithObjects:@"Three", @"Four", nil]
,   [NSArray arrayWithObjects:@"Nine", @"Two", nil]
,   nil];
NSPredicate *filter = [NSPredicate predicateWithBlock:^BOOL(id array, NSDictionary *bindings) {
    // This is the place where the condition is specified.
    // You can perform arbitrary testing on your nested arrays
    // to determine their eligibility:
    return [[array objectAtIndex:1] isEqual:@"Two"];
}];
NSArray *res = [data filteredArrayUsingPredicate:filter];
NSLog(@"%lu", res.count); 

顺便说一句,这与MySQL无关——这是一个通用的数据结构问题。数据库也是一种数据结构…:-)“我删除了MySQL和C标记,它们肯定是不相关的。”TheMan,读到。我甚至会说这是一个可能的重复。顺便说一句,第二个对象不在索引2!显然,它位于索引1。这就是为什么我在回答中写了“第三个对象”。顺便说一句,这与MySQL无关——这是一个通用的数据结构问题。数据库也是一种数据结构…:-)“我删除了MySQL和C标记,它们肯定是不相关的。”TheMan,读到。我甚至会说这是一个可能的重复。顺便说一句,第二个对象不在索引2!显然,它在索引一。这就是为什么我在我的答案中写了“第三个宾语”。这里谓词肯定会更好?这里谓词肯定会更好?