Re: Best way to create string from an array of dictionaries?
Re: Best way to create string from an array of dictionaries?
- Subject: Re: Best way to create string from an array of dictionaries?
- From: Keith Blount <email@hidden>
- Date: Mon, 24 Jul 2006 07:33:21 -0700 (PDT)
Many thanks for your reply. I have in fact gone with
something similar to your suggestion, iterating
through the array for the necessary values to make my
string. (I was just wondering about the most efficent
way of going about it given that this might be called
hundreds of times during a search.) I decided to
create a category on NSArray to do what I want, which
returns all values of any dictionary objects or the
object itself for other component types:
- (NSArray *)allComponentValues
{
NSMutableArray *allValues = [NSMutableArray array];
NSEnumerator *e = [self objectEnumerator];
id obj = nil;
while (obj = [e nextObject])
{
// If the object responds to -allValues, just add
all values; otherwise,
// add the object itself
if ([obj respondsToSelector:@selector(allValues)])
[allValues addObjectsFromArray:[obj allValues]];
else
[allValues addObject:obj];
}
return (NSArray *)allValues;
}
> Actually, I can't quit figure out why you just don't
> iterate through the array performing the match on
> each string. Surely, you don't want a search string
> to match across two keywords.
This is why the @"\n" is used to separate keywords
within the string - the search is performed via an
NSSearchField which sends its action on return, so it
will never search for the line break. The reason I
don't just iterate through the keywords array directly
and instead go to all the fuss of making a string is
that I am using an NSTableController subclass to
filter and display the search results that asks its
delegate for searchable keys of its record objects,
and it expects the value for all of these keys to be
an NSString that can be searched. It is easier to turn
the keywords array, which is the only object that
isn't already a string, into a string than it is to
change the logic of the array controller subclass to
handle different object types. Hope that makes sense!
Anyway, thanks again for your reply, much appreciated.
Best regards,
Keith
--- James Bucanek <email@hidden> wrote:
> Keith Blount wrote on Monday, July 24, 2006:
> >For now, I have done this:
> >
> >NSMutableString *str = [NSMutableString
> >stringWithString:[keywords
> >componentsJoinedByString:@"\n"]];
> > [str replaceOccurrencesOfString:@"keyword"
> >withString:@"" options:0 range:NSMakeRange(0,[str
> >length])];
> >
> >This gets rid of all of the occurrences of
> "keyword"
> >in the search string but obviously there are still
> the
> >junk characters. This is probably fine for my
> needs,
> >but I wondered if there was an easier or more
> efficent
> >way to go about this.
>
> Why not a simple loop?
>
> (Typed in e-mail)
>
> NSMutableString* s = [NSMutableString string];
> NSEnumerator* e = [keywords objectEnumerator];
> id dictionary;
> while ( (dictionary=[e nextObject])!=nil )
> [s appendFormat:@"\n%@",[dictionary
> objectForKey:@"keyword"]];
>
> (using a for loop and two more lines of code and you
> could eliminate the superfluous \n)
>
> Actually, I can't quit figure out why you just don't
> iterate through the array performing the match on
> each string. Surely, you don't want a search string
> to match across two keywords.
>
> NSEnumerator* e = [keywords objectEnumerator];
> id dictionary;
> while ( (dictionary=[e nextObject])!=nil )
> if ([[dictionary objectForKey:@"keyword"]
>
rangeOfString:@"search_string"].location!=NSNotFound)
> return (dictionary);
> return (nil);
> --
> James Bucanek
>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden