• 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: Dylan Adams <email@hidden>
  • Date: Sun, 13 Jul 2003 18:23:05 -0500

I would say that the ``easiest'' way to beautify your Objective-C code would be to use constants:

#define BookTitlePosition 0
#define BookAuthorPosition 1
#define BookPublisherPosition 2

/* ... */

Book *newBook = [[Book alloc] init];
NSArray *parts = [bookInfo componentsSeperatedByString: @", "];
[newBook setTitle: [parts objectAtIndex: BookTitlePosition]];
[newBook setAuthor: [parts objectAtIndex: BookAuthorPosition]];
[newBook setPublisher: [parts objectAtIndex: BookPublisherPosition]];

You can emulate the Perl's ability to assign into arrays by applying a category to NSArray:

@interface NSArray (GetObjectsIntoAddition)

/**
* assigns into the arguments from consecutive elements
* in the array, starting with element 0. Terminate the
* argument list with nil
*/
-(void)getObjectsInto: (NSObject **)firstObject, ...;

@end

@implementation NSArray (GetObjectsIntoAddition)

-(void)getObjectsInto: (NSObject **)firstObject, ...
{
if (firstObject != nil) {
va_list args;
NSObject **obj = firstObject;
int i = 0, count = [self count];

va_start(args, firstObject);
do {
*obj = [self objectAtIndex: i];
i++;
} while ((obj = va_arg(args, NSObject **)) != nil && i < count);

va_end(args);
}
}

@end

(Note: I just threw this code together. I did give it a quick test, but nothing serious. Since the name `getObjects' was already taken, I had to settle for `getObjectsInto,' which I don't really like. Also, it should probably complain if you've given it more things to assign to than items in the array. Usage of this code may cause your nose to fall off.)

Then your code could be:

Book *newBook = [[Book alloc] init];
NSArray *parts = [bookInfo componentsSeperatedByString: @", "];
NSString *author, *title, *publisher;

[parts getObjectsInto: &title, &author, &publisher, nil];
[newBook setTitle: title];
[newBook setAuthor: author];
[newBook setPublisher: publisher];

Another possibility would be to ask a different question. What if, instead of thinking about this problem in a Perlish way, where split and join are so common, you thought about this more in terms of C. In particular, I'm thinking of format strings. Let's say we have a class that looks like this:

@interface AttributeParser

+(BOOL)parse: (NSString *)data withFormat: (NSString *)format intoObject: (NSObject *)object;

@end

I think the best way to see what I'm trying to get at is to present a new version of your code, which would make use of this class:

Book *newBook = [[Book alloc] init];

[AttributeParser parse: bookInfo withFormat: @"%title%, %author%, %publisher%" intoObject: newBook];

The format is made up of field names, which are surrounded by %s, and character literals. As each data value is found, a set method using that field name is called on the object passed in. So, when AttributeParser found the data for %title%, it would call setTitle: with that value on newBook. This is probably over-engineering. And AttributeParser isn't a very good name.

dylan
_______________________________________________
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: Splitting a string and assigning to variables
  • 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