0

My NSMutableArray contains the following data

<DoctorInfo: 0x15de99e0> (entity: DoctorInfo; id: 0x15defbe0 <x-coredata://39D9B/DoctorInfo/p8> ; data: {
    doctorName = nil;
    emailAdd = nil;
    hospitalName = nil;
    mobileNumber = nil;
    phoneNumber = nil;
}),
<DoctorInfo: 0x15de9b00> (entity: DoctorInfo; id: 0x15da5dc0 <x-coredata://39D9BED3/DoctorInfo/p10> ; data: {
    doctorName = nil;
    emailAdd = nil;
    hospitalName = nil;
    mobileNumber = nil;
    phoneNumber = nil;
})
)

How can I remove those objects with nil? I have tried the following code by changing NSMutableArray to the NSArray and then filter it but it is still not working:

 NSString *predString = [NSString stringWithFormat:@"(doctorName BEGINSWITH[cd] '%@')", nil];

    NSPredicate *pred = [NSPredicate predicateWithFormat:predString];

     self.filteredDocInfoArray = [self.unfilteredDocInfoArray filteredArrayUsingPredicate:pred];
Anurag Sharma
  • 4,276
  • 2
  • 28
  • 44
SPUDERMAN
  • 187
  • 1
  • 1
  • 10
  • NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(DoctorInfo * evaluatedObject, NSDictionary *bindings) { if (evaluatedObject.doctorName == nil) { return YES; } return NO; }]; – teixeiras May 30 '17 at 13:08

2 Answers2

3

You test for nil in predicate strings like this:

NSPredicate *pred = [NSPredicate predicateWithFormat: @"doctorName = nil"];

See the documentation for examples.

jrturton
  • 118,105
  • 32
  • 252
  • 268
0

You cannot add a nil object to array. In your case you are adding an object that has some nil properties!

You can try this:

[yourArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

    DoctorInfo *customObj = obj;
    if (customObj.doctorName) {
        //add obj to temp array
    }
}];

Now you can replace yourArr with tempArr

If you want to use NSPredicate, please refer this.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109