0

I got following response from server:

[{"bp":"000/000","dateTime":"05/12/2016 01:02:59 PM","doc":{"email_id":"[email protected]","exception":0,"gender":"Male","id":0,"mobile_no":8055621745,"name":"Batra","profile_id":0,"qualification":"MD(Doctor)","reg_id":157,"salutation":"Mr","wellness_id":"251215782521"},"follow_up":"17","id":37,"medicine":["Syrup,Decold Total,20,0-0-1,Before Meal,1","Injection,Insulin,1,0-0-1,Before Meal,1","no","no","no","no","no","no","no","no"],"patient":{"email_id":"[email protected]","exception":0,"gender":"Male","id":0,"mobile_no":8055621745,"name":"Rohit","profile_id":0,"qualification":"","reg_id":150,"salutation":"Mr","wellness_id":"290119935030"},"weight":"000"}]

From that I have separate the medicine array like following way:

NSMutableArray *Myarray = [NSMutableArray new];
for (int i=0; i<_menuItems.count; i++) {
    [Myarray addObject:[[_menuItems objectAtIndex:i] objectForKey:@"medicine"]];
    NSLog(@"medicine: %@",Myarray);

I got output for this as like:

medicine: (
        (
        "Syrup,Decold Total,20,0-0-1,Before Meal,1",
        "Injection,Insulin,1,0-0-1,Before Meal,1",
        no,
        no,
        no,
        no,
        no,
        no,
        no,
        no
    )
)

Now what i want:

1) remove that all noelement.

2) after that, i want only 2nd element in each string.

in short i want my final output is like:

[Decold Total, Insulin];

But i don't know how to do that..??

Please anyone can solve my issue. help will be appreciable.

1 Answer 1

1

You need to use NSPredicate on Myarray and filter it.

Make your Myarray like this. NSMutableArray *Myarray = [NSMutableArray new];

for (int i=0; i<_menuItems.count; i++) {
     [Myarray addObjectsFromArray:[[_menuItems objectAtIndex:i] objectForKey:@"medicine"]];
}

1) Remove that all no element.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (SELF = %@)",@"no"];
NSArray *filterArray = [Myarray filteredArrayUsingPredicate:predicate];

2) Want only 2nd element in each string

NSMutableArray *medicineArray = [[NSMutableArray alloc] init];
for (NSString* medicine in filterArray) {
     NSArray *arr = [medicine componentsSeparatedByString:@","];
     if (arr.count >= 2)  {
          [medicineArray addObject:[arr objectAtIndex:1]];
     }
}
Sign up to request clarification or add additional context in comments.

5 Comments

filter: ( ( "Syrup,Decold Total,20,0-0-1,Before Meal,1", "Injection,Insulin,1,0-0-1,Before Meal,1", no, no, no, no, no, no, no, no ) ) This is outpot..
when i do like : if (Myarray.count>0) { NSLog(@"count: %u",Myarray.count);} it print count: 1
i just want my final output like [Decold Total, Insulin];.. what should i do for that..
@SurajSukale Check the edited answer and add object in your Myarray like i have done in for loop.
@SurajSukale Welcome mate :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.