• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Splitting a string and assigning to variables
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Follow-Ups:
    • Re: Splitting a string and assigning to variables
      • From: Jonathan Jackel <email@hidden>
    • Re: Splitting a string and assigning to variables
      • From: Dylan Adams <email@hidden>
    • Re: Splitting a string and assigning to variables
      • From: Thomas Finley <email@hidden>
References: 
 >Re: Splitting a string and assigning to variables (From: Jonathan Jackel <email@hidden>)

  • Prev by Date: Re: Question about NSRange on the G5 and the future sizeof(int).
  • Next by Date: Re: Splitting a string and assigning to variables
  • Previous by thread: Re: Splitting a string and assigning to variables
  • Next by thread: Re: Splitting a string and assigning to variables
  • Index(es):
    • Date
    • Thread