• 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: Jonathan Jackel <email@hidden>
  • Date: Sun, 13 Jul 2003 20:26:44 -0400

On Sunday, July 13, 2003, at 05:39 PM, Matt Diephouse wrote:

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.

You could use an NSScanner, which bypasses arrays to slice & dice the string. But code that uses scanners is often the definition of ugly.

Book *newBook = [[Book alloc] init];
NSString * bookInfo = @"Timeline, Michael Crichton, Knopf";
NSScanner *scanner = [NSScanner scannerWithString:bookInfo];
NSString *title, *author, *publisher;
[scanner scanUpToString:@"," intoString:&title];
[scanner scanString:"," intoString:nil];
[scanner scanUpToString:@"," intoString:&author];
[scanner scanString:"," intoString:nil];
[scanner scanUpToString:@"\n" intoString:&publisher];
[newBook setTitle:title];
[newBook setAuthor:author];
[newBook setPublisher:publisher];

Or you could get things into a dictionary pretty easily:

NSDictionary *newBook;
NSString * bookInfo = @"Timeline, Michael Crichton, Knopf";
NSArray *bookArray = [bookInfo componentsSeparatedByString:","];
NSArray *keyArray = [NSArray arrayWithObjects:@"title", @"author", @"publisher"];
newBook = [NSDictionary dictionaryWithObjects:bookArray forKeys:keyArray];

Don't feel weird about creating NSArrays for stuff like this. It might seem unnatural coming from Perl, but it is really, really common in Cocoa to create arrays on the fly for momentary use.

Jonathan
_______________________________________________
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.

References: 
 >Re: Splitting a string and assigning to variables (From: Matt Diephouse <email@hidden>)

  • Prev by Date: Re: Question about NSRange on the G5 and the future sizeof(int).
  • Next by Date: Found the document
  • 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