View Full Version : [Resolved] Using NSPredicate

May 28, 2011, 01:22 PM
I use the following to create an array of NSStrings:

self.dataArray = [[NSFileManager alloc] contentsOfDirectoryAtPath:self.pathToHere error:&error];

I would like to eliminate those strings that begin with Ď.í by using something like:

NSPredicate *noDotPredicate = [NSPredicate predicateWithFormat:@"SELF doesnotbeginwith'.'"];
NSArray *doesNotBeginWithDot = [self.dataArray filteredArrayUsingPredicate:noDotPredicate];

I am able to find beginswith but I canít find anything that looks like doesnotbeginwith

Any suggestions please?

May 28, 2011, 02:02 PM
Use the NOT operator.

Find the words Logical NOT on the above page.

You should probably read the entire Predicate Programming Guide, so you know where to place the NOT in the whole predicate expression.

May 28, 2011, 02:43 PM
Thanx Chown33.

I suspected that I would need to use the NOT keyword, but after reading the NSPredicate doc, I still don't see how to use it.

Two questions: how should I use NOT and where would I find that answer in the NSPredicate doc please?

A guess:
*noDotPredicate = [NSPredicate predicateWithFormat:@"SELF NOTtbeginswith'.'";]

Am I correct?

May 28, 2011, 03:12 PM
I suspected that I would need to use the NOT keyword, but after reading the NSPredicate doc, I still don't see how to use it.

Did you read this?

Look at the class reference doc for NSCompoundPredicate, method name starting with notPredicate

Also try google search terms: NSPredicate NOT example

A guess:
*noDotPredicate = [NSPredicate predicateWithFormat:@"SELF NOTtbeginswith'.'";]

Am I correct?
I see a 't' character between NOT and beginswith. Seems wrong to me, even without reading any reference doc or googling for examples.

Also, you can always test whether your guess is correct by writing code and running it. If it doesn't work the way you expect, then your guess isn't correct. Testing your guesses by writing test code is a good habit to get into.

May 28, 2011, 03:44 PM

Thanx ... the "t" is a cut and paste typo :)

I'll check out your suggestions ... thanx again.

May 28, 2011, 04:00 PM
I read the doc for NSCompoundPredicate so I think the following might be what I want:

NSPredicate *aPredicate = [NSPredicate predicateWithFormat:@"SELF beginswith'.'";]

NSPredicate *noDotPredicate = notPredicateWithSubpredicate:aPredicate;

or should it be:

NSPredicate *aPredicate = [NSPredicate predicateWithFormat:@"SELF beginswith'.'";]

NSPredicate *noDotPredicate = "NOT" aPredicate;

Do you agree?

May 28, 2011, 04:16 PM
Which of the posted code compiles? If it doesn't compile, can it possibly be correct?

Of the posted code that compiles, which of it runs and gives the expected result?

If you're trying to avoid compiling and testing the code, you're wasting your time. Those are things you're going to have to do anyway, so you might as well start doing them now.

May 28, 2011, 04:36 PM
Thanx CHOWN33.

The following worked:

NSPredicate *aPredicate = [NSPredicate predicateWithFormat:@"SELF beginswith'.'"];
NSPredicate *noDotPredicate = [NSCompoundPredicate notPredicateWithSubpredicate:aPredicate];
NSArray *tempData = [[NSFileManager alloc] contentsOfDirectoryAtPath:self.pathToHere error:&error];
self.dataArray = [tempData filteredArrayUsingPredicate:noDotPredicate];