NSArray中的NSPredicate,包含带“/”的数字

时间:2014-08-18 12:27:34

标签: ios nsmutablearray nsarray nspredicate nsnumber

我想用Facebook的用户生日日期过滤NSMutableArray。 我可以使用NSPredicate吗?我的NSLog"结果"是:

"birthday": "05/24/1993", 
  "id": "xxxxxxxxxxx""

我只想" 05/24/1993获取生日日期并将其保存在NSArray:生日。

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    FBRequest *friendRequest = [FBRequest requestForGraphPath:@"/me?fields=birthday"];
    [ friendRequest startWithCompletionHandler:^(FBRequestConnection *connection,id result, NSError *error) {
        [[NSUserDefaults standardUserDefaults]setObject:result forKey:@"birthdayDate"];
        self.birthdayArray=[[NSUserDefaults standardUserDefaults]objectForKey:@"birthdayDate"];

        NSPredicate *aPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] '/'"];
        self.birthday = [self.birthdayArray filteredArrayUsingPredicate:aPredicate];

        NSLog(@"BIRTHDAY : %@", self.birthday);

    }

NSArray生日的NSLog是错误的:

-[__NSCFDictionary filteredArrayUsingPredicate:]: unrecognized selector sent to instance 0x10f80a570

1 个答案:

答案 0 :(得分:0)

您的结果是NSDictionary实例,而不是NSArray实例。这就是为什么你收到错误消息,因为NSDictionary不支持filteredArrayUsingPredicate消息。

您可以获得包含NSDictionary中所有密钥的NSArray

NSArray *keys = [result allKeys];

像你一样过滤数组:

NSArray *filteredKeys = [keys filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] 'b'"]];

然后使用filteredKeys从字典中提取所需的值:

for (NSString *key in filteredKeys) { // Assuming that the dictionary keys are string
    id value = [result objectForKey:key];
    NSLog(@"Current value for key %@ = %@", key, value);
}