xcode 检查并从没有索引的 nmutable 数组中删除特定字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13933682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
check and remove a particular string from nsmutable array without index
提问by Selvaraj
In Xcode, I store some NSString
in NSMutableArray
.
在 Xcode 中,我将一些存储NSString
在NSMutableArray
.
Hello
Here
MyBook
Bible
Array
Name2
There
IamCriminal
User can enter string.
用户可以输入字符串。
Name2
I need to delete that particular string from NSMutableArray
without knowing index of the string. I have some idea that, Use iteration. Any other Best way. Plz Give with sample codings.
我需要在NSMutableArray
不知道字符串索引的情况下删除该特定字符串。我有一些想法,使用迭代。任何其他最佳方式。请提供示例代码。
回答by arthankamal
you can use containsObject
method in an NSMutableArray
你可以containsObject
在一个方法中使用NSMutableArray
if ([yourArray containsObject:@"object"]) {
[yourArray removeObject:@"object"];
}
回答by Endersstocker
[array removeObject:@"Name2"];
The documentation for NSMutableArray's removeObject: method states:
NSMutableArray 的 removeObject: 方法的文档说明:
matches are determined on the basis of an object's response to the isEqual: message
匹配是根据对象对 isEqual: 消息的响应确定的
In other words, this method iterates over your array comparing the objects to @"Name2"
. If an object is equal to @"Name2"
, it is removed from the array.
换句话说,此方法迭代您的数组,将对象与@"Name2"
. 如果对象等于@"Name2"
,则将其从数组中删除。
回答by Kalpesh
Try this,
尝试这个,
BOOL flag = [arrayName containsObject:Str];
if (flag == YES)
{
[arrayName removeObject:Str];
}
回答by MadhuP
[array removeObject:@"name2"];
回答by Talha
You can try this
你可以试试这个
for(NSString *string in array)
{
if([[string lowercasestring] isEqualToSring:[yourString lowercasestring]])
{
[array removeObject:string];
break;
}
}
回答by ask4asif
YOu can simply use
你可以简单地使用
[yourArray removeObject:stringObjectToDelete];
This method uses indexOfObject: to locate matches and then removes them by using removeObjectAtIndex:. Thus, matches are determined on the basis of an object's response to the isEqual: message. If the array does not contain anObject, the method has no effect.
此方法使用 indexOfObject: 来定位匹配项,然后使用 removeObjectAtIndex: 删除它们。因此,匹配是根据对象对 isEqual: 消息的响应来确定的。如果数组不包含 anObject,则该方法无效。