我有一个包含NSMutableDictionary值的NSMutableArray。我必须确定该NSMutableArray中是否存在特定的字典值?

NSMutableArray:

tableData:

{
    {

        categories = 267;
        "category_ids" = 267;
        "created_at" = "2012-04-18 05:32:10";
        "custom_design_from" = "2010-12-03 00:00:00";
    }
    {
        categories = 267;
        "category_ids" = 267;
        "created_at" = "2012-04-18 05:32:10";
        "custom_design_from" = "2010-12-03 00:00:00";
    }
 }

我必须确定一个特定的字典值。

我的代码:
if ( [tableData containsObject:[appDelegate.Product objectAtIndex:i]] )
{
     // Return Always false...
}

最佳答案

据我所知,containsObject:方法通过向其所有实例发送isEqual:消息来检查数组是否包含给定对象。 NSDictionary类具有方法isEqualToDictionary:比较字典内容。你可以试着像这样

- (BOOL) dictionaryArray:(NSMutableArray*)array containsDictionary:(NSDictionary*)givenDictionary
{
    BOOL result = NO;

    for( NSDictionary* dict in array )
    {
        if( [dict isEqualToDictionary:givenDictionary] )
        {
            result = YES;
        }
    }

    return result;
}

如果您的数组通过调用包含给定的字典,则此方法应返回
NSDictionary* dict = [appDelegate.Product objectAtIndex:i];
if ( [self dictionaryArray:tableData containsDictionary:dict] )
{
    // do what you need
}

关于iphone - 比较两个NSMutableArray,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10913735/

10-16 20:00