Splitting a string and assigning to variables
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.