Search code examples
objective-ciosxcodensstringnsmutablearray

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.


Solution

  • 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" } 
    }