Resolved Using NSPredicate

Discussion in 'iOS Programming' started by multinode, May 28, 2011.

  1. multinode, May 28, 2011
    Last edited: May 28, 2011

    macrumors regular

    Joined:
    Feb 4, 2011
    #1
    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?
     
  2. chown33, May 28, 2011
    Last edited: May 28, 2011

    macrumors 603

    Joined:
    Aug 9, 2009
    #2
  3. thread starter macrumors regular

    Joined:
    Feb 4, 2011
    #3
    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?
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
    #4
    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


    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.
     
  5. thread starter macrumors regular

    Joined:
    Feb 4, 2011
    #5
    Using NSPredicate

    Hi CHOWN33

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

    I'll check out your suggestions ... thanx again.
     
  6. multinode, May 28, 2011
    Last edited: May 28, 2011

    thread starter macrumors regular

    Joined:
    Feb 4, 2011
    #6
    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?
     
  7. macrumors 603

    Joined:
    Aug 9, 2009
    #7
    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.
     
  8. multinode, May 28, 2011
    Last edited: May 28, 2011

    thread starter macrumors regular

    Joined:
    Feb 4, 2011
    #8
    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];
    
     

Share This Page