0

I am trying to remove any elements from an NSMutableArray object that contain a SPACE using the following:

[searchCriteria removeObject:[NSString stringWithString:@" "]];

I get the exception "-[__NSArrayI removeObject:]: unrecognized selector sent to instance". What am I doing wrong?

EDIT: OK, so I tried the following

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF NOT CONTAINS ' '"];
NSArray *nonEmptyElements = [searchCriteria filteredArrayUsingPredicate: predicate];

and get the error Unable to parse the format string "SELF NOT CONTAINS ' '"'

How do I specify an empty string after the CONTAINS?

ChrisP
  • 9,796
  • 21
  • 77
  • 121

2 Answers2

4

You have a plain NSArray rather than an NSMutableArray. Probably at some point the NSMutableArray you thought you had was copied or serialized, resulting in an immutable array.

Incidentally, as Lefteris says, this will filter out strings consisting only of a single space. To filter out any string that has a space anywhere in it, you'll want to do it the way he says. And if you do want to filter out single-space strings, it's simpler just to write @" " instead of [NSString stringWithString:@" "] — it's precisely equivalent.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • I do want to remove elements consisting of just an empty space. The array is created using (NSMutableArray*)[searchText componentsSeparatedByString:@" "] which should cast the result to an NSMutableArray correct? – ChrisP May 23 '12 at 01:17
  • 2
    @ChrisP: Nah, you can't cast objects like that. Objects are always represented by pointers, and casting a pointer from one type to another doesn't change the actual object it points to. To convert your array to an NSMutableArray, you'll need to get one from `[searchText mutableCopy]` (and then don't forget to release the mutable array when you're done if you aren't using ARC). – Chuck May 23 '12 at 01:27
  • Thanks Chuck for that clarification. That turned out to be the root of the problem. I'm coming from C# where that cast would have actually converted searchText to be mutable. – ChrisP May 23 '12 at 12:14
  • @ChrisP: Yeah, it surprises a lot of people. The thing about Objective-C is that for the most part it's just C with a little bit of new syntax for defining classes and sending messages. In general, a feature is either C or Objective-C, and C doesn't know about objects. So C things like casting still work the same way as in C. C# didn't have this baggage, so it could bake objects into the language's core. (Objective-C is moving more in that direction with, for example, named properties and boxing support, but it's just now getting these things as the language approaches its 30th birthday!) – Chuck May 23 '12 at 18:17
2

You should use an NSPredicate to filter out any items with space. See here how to do this: https://stackoverflow.com/a/1057202/312312

You should use the self contains predicate though, instead of self beginswith that the example uses.l

Community
  • 1
  • 1
Lefteris
  • 14,550
  • 2
  • 56
  • 95
  • Although it turns out I don't need to use NSPredicate it is helpful to know about it's functionality. Thanks Lefteris. – ChrisP May 23 '12 at 12:15