Selvaraj
Selvaraj

Reputation: 726

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.

Upvotes: 8

Views: 11720

Answers (6)

arthankamal
arthankamal

Reputation: 6413

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

Upvotes: 22

Kalpesh
Kalpesh

Reputation: 5334

Try this,

BOOL flag = [arrayName containsObject:Str];   

if (flag == YES)
{   
     [arrayName removeObject:Str];
}

Upvotes: 0

ask4asif
ask4asif

Reputation: 676

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.

Upvotes: 0

Bryan E
Bryan E

Reputation: 1091

[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.

Upvotes: 8

Talha
Talha

Reputation: 809

You can try this

for(NSString *string in array)
{
    if([[string lowercasestring] isEqualToSring:[yourString lowercasestring]])
    {
        [array removeObject:string];
        break;
    }
}

Upvotes: 0

MadhuP
MadhuP

Reputation: 2019

[array removeObject:@"name2"];

Upvotes: 0

Related Questions