objective c - Matching a string against multiple patterns -
i have question regarding style of solution pretty simple problem.
i have program matches list of file names against number of patterns. if file name matches pattern, file renamed , counter incremented.
currently i'm matching against 4 different patterns
if ([file rangeofstring:pattern].location != nsnotfound) { counter ++; //rename file... } if ([file rangeofstring:pattern2].location != nsnotfound) { counter2 ++; //rename file... } [...]
the solution works not scale. if have match against more patterns.
so thought using like
nsstring *someregexp = ...; nspredicate *mytest = [nspredicate predicatewithformat:file, someregexp]; if ([mytest evaluatewithobject: teststring]){ }
however, not see way increment counters in such solution depend on exact match....
so wondering whether here knows more comprehensive/nice solution problem.....
thanks in advance norbert
how subclassing nsstring, adding counter track match count
.
@interface patternmatchingstring : nsstring @property (readwrite) nsuinteger matchcount; @end -(void)patternmatching { for(patternmatchingstring *pattern in arrayofpatterns) { if([file rangeofstring:pattern].location != nsnotfound) pattern.matchcount++; } }
Comments
Post a Comment