1

Following is the array of dictionary:

    {
DATA: [
{

GAMEID: 509782,
FIELDNUMBER: "KPB #1",
},
{

GAMEID: 509782,
FIELDNUMBER: "KPB #1",
},
{

GAMEID: 509782,
FIELDNUMBER: "KPB #1",

},
{...

Now I want to filter the array based on GAMEID. For this I am creating a loop and inside that am using NSPredicate. Following is my code:

    func callSort(arr:NSMutableArray) -> NSMutableDictionary {
        var arrId = arr.valueForKey("GAMEID")
        for(index, ele) in arrGames.enumerate() {
            let str = ele.valueForKey("GAMEID")
            let pred = NSPredicate(format: "ANY DATA.GAMEID == %@", argumentArray: str) // error on this line
            var filterArr =  arrGames.filteredArrayUsingPredicate(pred)

        }

        return sortedDict
    }

The error I get is:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse function name 'Optional:' into supported selector (Optional:)

I am not sure if I am using the NSPredicate the right way.

Where am I getting wrong? How do I solve this?

Edited Code

let pred = NSPredicate(format: "GAMEID = %d", argumentArray: [str!])

            var filterArr = arrGames.filteredArrayUsingPredicate(pred)
z22
  • 10,013
  • 17
  • 70
  • 126
  • possible duplicate of [How to filter NSArray using predicate on an object property](http://stackoverflow.com/questions/19676371/how-to-filter-nsarray-using-predicate-on-an-object-property/19676711#19676711) – Dipen Panchasara Dec 17 '15 at 05:45
  • @DipenPanchasara I want the exact match of GAMEID and hence don't want to use `contains`. – z22 Dec 17 '15 at 05:53
  • first question @z22, do you want to sort or filter? `sort`(ordering) is different than `filter`(search). – Dipen Panchasara Dec 17 '15 at 05:56
  • @DipenPanchasara I wan to filter out elements(group) based on id – z22 Dec 17 '15 at 05:59
  • then first you need to find distinct object and then filter it with matching object. because people can guide you, but not sure provide code. – Dipen Panchasara Dec 17 '15 at 06:03
  • you may check this post [Grouping of Custom Objects in Objective-C](http://stackoverflow.com/questions/10816458/grouping-of-custom-objects-in-objective-c). i hope you get guidance. – Dipen Panchasara Dec 17 '15 at 06:05
  • @DipenPanchasara Can you pls check my edited code, I am now ale to fetch teh data using this but all data is not matched, For eg. instead of 5 matching records, only 3 are fetched. Any idea why – z22 Dec 17 '15 at 06:36

1 Answers1

0

Hi i solve this issue in objective c i had same JSON format could you refer this Searching NSArray of NSDictionary objects answer? i had used it for search functionality hope this will help you out

Community
  • 1
  • 1
Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68