gpt4 book ai didi

objective-c - 是否有相当于 Smalltalk 的#copyWithout : in Cocoa’s collection classes?

转载 作者:行者123 更新时间:2023-12-03 17:51:33 24 4
gpt4 key购买 nike

在 Smalltalk 中,我习惯于制作没有不需要的元素的集合的副本,如下所示:

myCollection copyWithout: undesiredObject

它应答不包含任何等于undesiredObject的元素的接收者副本。

Cocoa 中有类似的东西吗?
如果没有,实现这样的副本的最佳方法是什么?
我对复制 NSSet 实例特别感兴趣。

最佳答案

Cocoa Foundation 类中没有直接等效的内容。

可能的方法#1:

NSSet *withoutUndesiredObject = [myCollection objectsPassingTest:^BOOL(id obj, BOOL *stop) {
return ![obj isEqualTo:undesiredObject];
}];

可能的方法#2:

NSMutableSet *withoutUndesiredObject = [myCollection mutableCopy];
[withoutUndesiredObject removeObject:undesiredObject];

其他集合类也存在类似的方法NSArrayNSDictionary

关于objective-c - 是否有相当于 Smalltalk 的#copyWithout : in Cocoa’s collection classes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25248469/

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