簡體   English   中英

如何從與NSNumber匹配的NSMutableArray中刪除對象

[英]How do I remove objects from an NSMutableArray matching an NSNumber

我有一個像這樣的數組:

    {
    id = 8281;
    name = “John”;
    title = “Title One“;
     },
    {
    id = 8729;
    name = “Bob”;
    title = “Title Two“;
    },
    {
    id = 8499;
    name = “Dave”;
    title = “Title Three“;
    }

我想刪除包含特定ID的數組。

例如,假設我有:

NSNumber *removeThis =  '8281' ; 

我上面的數組被命名為“故事”。

我努力了:

[stories removeObject:removeThis];

但這不起作用。

我也嘗試過:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id != %@", removeThis];
NSArray *results = [stories filteredArrayUsingPredicate:predicate];

但是數據永遠不會從數組中刪除。

有人可以指出我正確的方向嗎?

我不這樣認為:

NSNumber *removeThis =  '8281'; 

...甚至是有效的Objective-C。 使用@(number)生成NSNumber文字,例如:

NSNumber *removeThis =  @(8281); 

從那里開始,它應該與您鍵入的內容完全一樣:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id != %@", removeThis];
NSArray *results = [stories filteredArrayUsingPredicate:predicate];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM