Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

multinode

macrumors regular
Original poster
Feb 4, 2011
150
0
I use the following to create an array of NSStrings:

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

I would like to eliminate those strings that begin with ‘.’ by using something like:

Code:
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?
 
Last edited:
Using NSPredicate

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:mad:"SELF NOTtbeginswith'.'";]

Am I correct?
 
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?
http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/Predicates/Articles/pBNF.html

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:mad:"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.
 
Using NSPredicate

Hi CHOWN33

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

I'll check out your suggestions ... thanx again.
 
I read the doc for NSCompoundPredicate so I think the following might be what I want:



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

NSPredicate *noDotPredicate = notPredicateWithSubpredicate:aPredicate;

or should it be:

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

NSPredicate *noDotPredicate = "NOT" aPredicate;

Do you agree?
 
Last edited:
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.
 
Using NSPredicate

Thanx CHOWN33.

The following worked:

Code:
	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];
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.