ios - 如何从 NSMutableArray 中删除 nil 对象?

标签 ios objective-c

我的 NSMutableArray 包含以下数据

<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];

最佳答案

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

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

参见 the documentation举些例子。

关于ios - 如何从 NSMutableArray 中删除 nil 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44262980/

相关文章:

ios - 不能将 UISearchController 与 UICollectionView 一起使用吗?

iphone - 为推送通知购买服务器

ios - 设置 self 时无法正确更新 float 的 UILabel 文本的函数(仅适用于 super)

ios - 无法识别NSJSONSerialization

objective-c - 如何在 Objective-C 中调用方法?

ios - Google 登录集成问题

android - React Native 在应用内发送短信

iphone - 获取当前用户位置的地址

ios - 压缩视频的尺寸/秒转换率是多少

ios - 在 UITextView 上以编程方式禁用 iOS8 Quicktype 键盘