【问题标题】:Removing NSString form NSMutableArray从 NSMutableArray 中删除 NSString
【发布时间】:2013-12-29 02:42:18
【问题描述】:

我有一个包含 3 个字符串的数组,它们都等于“Farm”

当我尝试使用这行代码只删除一个“农场”时:

[array removeObject:@"Farm"];

它会删除所有内容。

我怎样才能只删除一个?

【问题讨论】:

  • 找到一个并按数字而不是值将其删除。

标签: ios nsstring nsmutablearray


【解决方案1】:

首先,您只需要获取“农场”字符串之一的索引。如果找到索引,则可以从数组中删除该索引处的对象。

NSUInteger index = [array indexOfObject:@"farm"];
if (index!=NSNotFound) {
    [array removeObjectAtIndex:index];
}

【讨论】:

    【解决方案2】:

    removeObject 表示删除给定对象数组中的所有匹配项

    查看说明:

    此方法使用 indexOfObject: 定位匹配项,然后使用 removeObjectAtIndex: 将其删除。

    所以,请使用 removeObjectAtIndexremoveLastObject

    【讨论】:

      【解决方案3】:

      试试这样的...

      [array removeObjectAtIndex:<index of object to delete>];

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-03
        • 1970-01-01
        • 2011-02-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多