Re: Splitting a string and assigning to variables
Re: Splitting a string and assigning to variables
- Subject: Re: Splitting a string and assigning to variables
- From: Matt Diephouse <email@hidden>
- Date: Sun, 13 Jul 2003 17:39:02 -0400
On Sunday, July 13, 2003, at 05:30 PM, Jonathan Jackel wrote:
On Sunday, July 13, 2003, at 04:00 PM, Matt Diephouse wrote:
Book *newBook = [[Book alloc] init];
NSArray *parts = [bookInfo componentsSeperatedByString:", "];
[newBook setTitle: [parts objectAtIndex:0]];
[newBook setAuthor: [parts objectAtIndex:1]];
[newBook setPublisher: [parts objectAtIndex: 2]];
I think that's a bit ugly though. I hope there's a better way. Is
there? Thanks.
The ugliness is the result of your data (bookInfo = @"Timeline,
Michael Crichton, Knopf") being ugly. If your data started as a
dictionary you could do this a little more elegantly. Arrays and
strings don't know as much about their contents as dictionaries.
OTOH, dictionaries take some effort to make in the first place.
Exactly what do you mean by "ugly" and "better" anyway?
Jonathan
I'm just thinking about how I would do this if I was programming in
Perl. I don't like having to call an array to get my values when
passing them to the Book object. This is what I'd do in Perl:
my ($title, $author, $publisher) = split ", ", $bookInfo;
$newBook->setTitle($title);
$newBook->setAuthor($author);
$newBook->setPublisher($publisher);
This quite obviously isn't Perl, though. I'm still getting used to
Objective-C. I just find the use of variables more descriptive than the
indices of an array.
matt
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.