Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check and remove a particular string from nsmutable array without index

In Xcode, I store some NSString in 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.

like image 811
Selvaraj Avatar asked Dec 18 '12 13:12

Selvaraj


2 Answers

you can use containsObject method in an NSMutableArray

if ([yourArray containsObject:@"object"]) {
    [yourArray removeObject:@"object"];
}

Swift:

if yourArray.contains("object") {
    yourArray = yourArray.filter{ $0 != "object" } 
}
like image 167
arthankamal Avatar answered Oct 23 '22 06:10

arthankamal


[array removeObject:@"Name2"];

The documentation for NSMutableArray’s removeObject: method states:

matches are determined on the basis of an object’s response to the isEqual: message

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.

like image 30
Endersstocker Avatar answered Oct 23 '22 07:10

Endersstocker