• 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: Better way to create a 'grouped' array from a NSArray?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Better way to create a 'grouped' array from a NSArray?


  • Subject: Re: Better way to create a 'grouped' array from a NSArray?
  • From: Seth Willits <email@hidden>
  • Date: Tue, 26 Mar 2013 10:47:02 -0700

On Mar 26, 2013, at 1:12 AM, Diederik Meijer | Ten Horses wrote:

> But I'd like to know if there is a quicker or more efficient way to do this, with less code. If so, please let me know.



More efficient, yes. Better? Different way to look at it at least:


NSArray * orderedHits = [[result valueForKeyPath:@"hits.hits"] sortedArrayUsingComparator:^(id a, id b){
	NSString * aDate = [a valueForKeyPath:@"_source.datum_gepubliceerd_ymd"];
	NSString * bDate = [b valueForKeyPath:@"_source.datum_gepubliceerd_ymd"];
	return [bDate compare:aDate options:NSNumericSearch];
}];

NSMutableArray * dates = [NSMutableArray array];
NSMutableArray * content = [NSMutableArray array];
NSMutableArray * contentForCurrentDate = nil;
NSDate * currentDate = nil;

for (NSDictionary * dict in orderedHits) {
	NSString * date = [dict valueForKeyPath:@"_source.datum_gepubliceerd_ymd"];
	if (date) {
		if (date != currentDate) {
			contentForCurrentDate = [NSMutableArray array];
			[content addObject:contentForCurrentDate];
			[dates addObject:date];
		}

		[contentForCurrentDate addObject:dict];
	}
}

self.dates = dates;
self.content = contentByDate;



Also, it's generally a very bad idea to expose a mutable container in a property (ie, your self.datesArray and self.sortedJSONContent). You will hate yourself later if you run into one of several problems you can easily get yourself into. For example, you can easily mutate the container behind an object's back which can lead to it exploding, and you also lose KVO compatibility. It's better to expose an immutable container and possibly convenience methods to mutate (of either the KVC variety, or ones that at least will/didChangeKey: manually).


--
Seth Willits





_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Better way to create a 'grouped' array from a NSArray? (From: Diederik Meijer | Ten Horses <email@hidden>)

  • Prev by Date: Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]
  • Next by Date: Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]
  • Previous by thread: Better way to create a 'grouped' array from a NSArray?
  • Next by thread: custom insertion point
  • Index(es):
    • Date
    • Thread