0

我正在尝试使用 NSPredicate 从数组中过滤数据。我的数组(arrLikes)包含来自服务器的以下数据

{
    "id":17,
    "likes":[
        {
        "likedBy":
            {
             "firstName":"Bob",
             "lastName":"Builder",
             "id":1
            }
        }],
    "comments":[]
}

我正在尝试从数组中过滤数据,如下所示

long loggedInUserId = [[dictUserInfo objectForKey:@"id"] longValue];
NSPredicate *likePredicate = [NSPredicate predicateWithFormat:@"id == %ld", loggedInUserId ];
NSArray *filteredLikeArray  = [arrLikes filteredArrayUsingPredicate:likePredicate];

即使我的 loggedInUserId 为 1,filteredLikeArray 计数也会返回 0。上面的代码有什么问题?任何形式的帮助表示赞赏。谢谢。

4

1 回答 1

0

您是否尝试将 long 转换为 NSString 如下?

long loggedInUserId = 17;
NSString *stringUserId = [NSString stringWithFormat:@"%ld", loggedInUserId];
NSPredicate *likePredicate = [NSPredicate predicateWithFormat:@"id == %@", stringUserId];
NSArray *filteredLikeArray  = [arrLikes filteredArrayUsingPredicate:likePredicate];
于 2012-11-27T07:46:55.513 回答