如何从NSMutableArray中删除nil对象?

时间:2017-05-30 12:59:37

标签: ios objective-c

我的<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; }) ) 包含以下数据

nil

如何使用NSMutableArray删除这些对象?我通过将NSArray更改为 NSString *predString = [NSString stringWithFormat:@"(doctorName BEGINSWITH[cd] '%@')", nil]; NSPredicate *pred = [NSPredicate predicateWithFormat:predString]; self.filteredDocInfoArray = [self.unfilteredDocInfoArray filteredArrayUsingPredicate:pred]; 然后对其进行过滤来尝试了以下代码,但它仍无效:

IEnumerable<Registry> registries = GetTableData();
string oneofthedesiredname = registries
.First()
.Attachments
.First()
.AttachmentType.Name; //I want that, this definition works but its null

2 个答案:

答案 0 :(得分:3)

你在这样的谓词字符串中测试nil:

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

有关示例,请参阅the documentation

答案 1 :(得分:0)

您无法将nil对象添加到数组中。在你的情况下,你添加一个具有一些零属性的对象!

你可以试试这个:

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

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

现在您可以用tempArr

替换yourrrr

如果您想使用NSPredicate,请参阅this.