• 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
Splitting a string and assigning to variables
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Splitting a string and assigning to variables


  • Subject: Splitting a string and assigning to variables
  • From: Matt Diephouse <email@hidden>
  • Date: Sun, 13 Jul 2003 16:00:02 -0400

I'm a little lost as to how to do the following in Objective-C. Let's say I have a string some information about a book.

// book title, author, and publisher
NSString *bookInfo = @"Timeline, Michael Crichton, Knopf";

Now I want to take this information and feed it to a book object.

@interface Book : NSObject {
NSString *title;
NSString *author;
NSString *publisher;
}
- (void)setTitle: (NSString *)given;
- (void)setAuthor: (NSString *)given;
- (void)setPublisher: (NSString *)given;
@end

Given a newly allocated and initialized Book object, how do I set the information? I could do it like this:

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.

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>
  • Prev by Date: Re: Question about NSRange on the G5 and the future sizeof(int).
  • Next by Date: About a bug in Jaguar
  • Previous by thread: ABLocalizedPropertyOrLabel does not localize
  • Next by thread: Re: Splitting a string and assigning to variables
  • Index(es):
    • Date
    • Thread