gpt4 book ai didi

ios - 是否有检查 "All elements of an array contains [NSNULL null]"类型对象的内置函数?

转载 作者:行者123 更新时间:2023-11-29 11:20:06 25 4
gpt4 key购买 nike

我有一个 NSMutableArray,它包含 100 个 [NSNULL null] 类型的对象。

但有时它包含 1 个有效对象(它可能是一个 NSString)和 99 个 [NSNULL null] 类型对象(它可能会根据情况而有所不同)。

我可以知道是否有任何内置函数可以检查数组的所有元素是否包含 [NSNULL null] 类型对象?(或者它不包含任何一个有效对象。)

谢谢。

注意:没有使用循环语句迭代所有元素。

最佳答案

您可以使用 NSPredicate 来实现这一点。

NSMutableArray *allObjects = /* Assume this is your main array */;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self = nil"];
NSArray *nullObjects = [allObjects filteredArrayUsingPredicate:predicate];

if ([nullObjects count] == [allObjects count]) {
// All objects are [NSNull null]
} else {
// Some objects are of different types(may be NSString)
}

关于ios - 是否有检查 "All elements of an array contains [NSNULL null]"类型对象的内置函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7672102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com