[英]How do I remove objects from an NSMutableArray matching an NSNumber
I have an array like this: 我有一个像这样的数组:
{
id = 8281;
name = “John”;
title = “Title One“;
},
{
id = 8729;
name = “Bob”;
title = “Title Two“;
},
{
id = 8499;
name = “Dave”;
title = “Title Three“;
}
I want to remove the array containing a specific ID. 我想删除包含特定ID的数组。
As an example, let's say I have: 例如,假设我有:
NSNumber *removeThis = '8281' ;
And my array above is named "stories". 我上面的数组被命名为“故事”。
I have tried: 我努力了:
[stories removeObject:removeThis];
but that does not work. 但这不起作用。
I have also tried: 我也尝试过:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id != %@", removeThis];
NSArray *results = [stories filteredArrayUsingPredicate:predicate];
But the data is never removed from the array. 但是数据永远不会从数组中删除。
Can someone point me in the right direction? 有人可以指出我正确的方向吗?
I don't think this: 我不这样认为:
NSNumber *removeThis = '8281';
...is even valid Objective-C. ...甚至是有效的Objective-C。 Use
@(number)
to make an NSNumber
literal, like: 使用
@(number)
生成NSNumber
文字,例如:
NSNumber *removeThis = @(8281);
From there, it should work exactly as you typed it: 从那里开始,它应该与您键入的内容完全一样:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id != %@", removeThis];
NSArray *results = [stories filteredArrayUsingPredicate:predicate];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.