1

NSPredicateを使用してこの配列(NSDictionariesでいっぱい)をフィルタリングしようとしています...

動作していないコードが非常に少量あります...

次のコードはlabel.textをAmyBurnett34に変更する必要がありますが、そうではありません...

NSPredicate *pred = [NSPredicate predicateWithFormat:@"id = %@", [[mightyPlistDict objectForKey:@"pushesArr"] objectAtIndex:indexPath.row]];

    NSLog(@"%@",pred);

    label.text = [[[twitterInfo filteredArrayUsingPredicate:pred] lastObject] objectForKey:@"screen_name"];
    NSLog(@"%@",twitterInfo);

そしてこれがNSLogedを取得するものです...

2012-08-05 11:39:45.929 VideoPush[1711:707] id == "101323790"
2012-08-05 11:39:45.931 VideoPush[1711:707] (
        {
        id = 101323790;
        "screen_name" = AmyBurnett34;
    },
        {
        id = 25073877;
        "screen_name" = realDonaldTrump;
    },
        {
        id = 159462573;
        "screen_name" = ecomagination;
    },
        {
        id = 285234969;
        "screen_name" = "UCB_Properties";
    },
        {
        id = 14315150;
        "screen_name" = MichaelHyatt;
    }
)

これもNSLogの場合は、注意が必要です...配列は空です...

NSLog(%@,[twitterInfo filteredArrayUsingPredicate:pred]);
4

3 に答える 3

2

問題は、述語が文字列との比較を使用しており、コンテンツが数値を使用していることです。これを試して:

NSNumber *idNumber = [NSNumber numberWithLongLong:[[[mightyPlistDict objectForKey:@"pushesArr"] objectAtIndex:indexPath.row] longLongValue]];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"id = %@", idNumber];
于 2012-08-05T16:25:23.390 に答える
0

「id」の値が文字列であるかどうかはわかりません。NSNumberである可能性があります。私は提案します:

NSUInteger matchIdx = ...;
NSUInteger idx = [array indexOfObjectPassingTest:^BOOL(NSDictionary *dict, NSUInteger idx, BOOL *stop)
{
  id obj = [dict objectForKey:@"id"];
  // NSLog the class if curious using NSStringFromClass[obj class];
  NSUInteger testIdx = [obj integerValue]; // works on strings and numbers
  return testIdx == matchIdx;
}
if(idx == NSNotFound) // handle error

NSString *screenName = [[array objectAtIndex:idx] objectForKey:@"screen_name"];
于 2012-08-05T16:31:43.837 に答える
-1

NSPredicateは、配列を並べ替えるのではなく、配列をフィルタリングするために使用されます。配列をソートするには、NSArrayのメソッドを使用します。sortedArrayUsingDescriptors

例:

// Define a sort descriptor based on last name.
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];    

// Sort our array with the descriptor.
NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
于 2012-08-05T16:22:06.030 に答える