Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]

2013-03-26 Thread Quincey Morris
On Mar 26, 2013, at 14:14 , Conrad Shultz  wrote:

> Step #1 in my hypothetical was that in the future a *required* delegate 
> method be added, in which case there would be no -respondsToSelector: check.

1. Your earlier suggestion of having *two* delegate objects would certainly be 
one way of ensuring this couldn't be a problem.

2. If there is to be only one delegate object, the correct way (in terms of 
ensuring compile-time compliance) would be to declare the subclass "delegate" 
property override like this:

@property (nonatomic) id delegate; // making it 
readwrite now

That is, any code setting a delegate would be required to provide one of the 
correct sort. If desirable (and perhaps it would be under the circumstances), 
this could be enforced at run-time by providing overrides, rather than using 
@dynamic:

- (id) delegate {
id delegate = [super delegate];
if ([delegate conformsToProtocol: @protocol 
(TKOutlineViewDelegate)])
return delegate;
else
return nil;
}

- (void) setDelegate: (id) delegate {
NSAssert ([delegate conformsToProtocol: @protocol 
(TKOutlineViewDelegate)], @"Boo boo");
[super setDelegate: delegate];
}

3. With one delegate object, if there's a legal scenario where some code might 
think that the outline view was merely a NSOutlineView and therefore set a 
delegate that was merely NSOutlineViewDelegate rather than 
TKOutlineViewDelegate, then conformity to TKOutlineViewDelegate is effectively 
optional. It would be incumbent, then, on callers of the TKOutlineViewDelegate  
delegate methods to either check for conformity or to check respondsToSelector, 
even for @required methods.

The correct approach requires knowing the answer to this question: Does 
TKOutlineView *require* TKOutlineViewDelegate conformity, or is it merely 
optional?

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


custom insertion point

2013-03-26 Thread Peng Gu
Hi,

I'm changing the insertion point size by overriding
*-(void)drawInsertionPointInRect:(NSRect)aRect
color:(NSColor *)aColor turnedOn:(BOOL)flag, *But it doesn't handle the
first blink (when you move the insertion point, it goes back to normal)

I managed to handle the first blink by overriding the private method *- (
void)_drawInsertionPointInRect:(NSRect)aRect color:(NSColor *)aColor.*
*
*
But this is not a solution for me since overriding the private method will
result in being decline by App Store. I want the app to be in App Store. I
see Apps like iAWriter and Writeroom have a custom insertion point and they
are in App store.

Does anyone know how they managed to do this, or a better way rather than
overriding the private method?

Thanks.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]

2013-03-26 Thread Conrad Shultz

On Mar 26, 2013, at 2:05 PM, Marco S Hyman  wrote:

>> 3) The TKOutlineView then later attempts to send the  
>> message from #1 above to its delegate.
> 
> The code noted earlier used introspection to ensure the delegate responded
> to the selector before the message was sent.  A non TKOutlineViewDelegate
> conforming object won't respond to the selector.  The message won't be
> sent.  No exception will be raised.
> 
> Or did I miss something?
> 
> Marc


As currently written it should work. Step #1 in my hypothetical was that in the 
future a *required* delegate method be added, in which case there would be no 
-respondsToSelector: check.

If no methods are required in the protocol then my concern would not apply.

-Conrad
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]

2013-03-26 Thread Marco S Hyman
> 3) The TKOutlineView then later attempts to send the  
> message from #1 above to its delegate.

The code noted earlier used introspection to ensure the delegate responded
to the selector before the message was sent.  A non TKOutlineViewDelegate
conforming object won't respond to the selector.  The message won't be
sent.  No exception will be raised.

Or did I miss something?

Marc
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]

2013-03-26 Thread Conrad Shultz

On Mar 26, 2013, at 1:40 PM, Quincey Morris 
 wrote:

> On Mar 26, 2013, at 13:11 , Conrad Shultz  wrote:
> 
>> If code expecting an NSOutlineView receives a TKOutlineView instance it may 
>> break or behave unexpectedly since it may well try to set a delegate 
>> conforming to  but you have made your class require a 
>> delegate conforming to the more specific .
> 
> Chris's delegate actually implements NSOutlineViewDelegate, doesn't it?
> 
> Lee Ann's earlier suggestion of declaring TKOutlineViewDelegate to conform to 
> NSOutlineViewDelegate would prevent any remaining compiler complaints. Thus 
> the final declaration of the protocol would look like:
> 
>   @protocol TKOutlineViewDelegate 
> 
> Would that take care of your concern?


My concern is not a compile-time issue; it's a substitutability issue that may 
manifest at run-time.

Consider the following hypothetical:

1) A required method is added to .

2) A class (say, a view controller) expecting an NSOutlineView is handed a 
TKOutlineView and sets its delegate to an object conforming to 
 but *not* . This is valid since 
TKOutlineView is a subclass of NSOutlineView, and as such should be 
substitutable anywhere an NSOutlineView is expected.

3) The TKOutlineView then later attempts to send the  
message from #1 above to its delegate.

4) An exception is raised at run-time since the delegate actually conforms to 
, not .

Admittedly this is a fairly specific combination of events that may never 
apply, but I wanted to make sure the OP is aware of it.

-Conrad
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]

2013-03-26 Thread Seth Willits
On Mar 26, 2013, at 1:40 PM, Quincey Morris wrote:

>> If code expecting an NSOutlineView receives a TKOutlineView instance it may 
>> break or behave unexpectedly since it may well try to set a delegate 
>> conforming to  but you have made your class require a 
>> delegate conforming to the more specific .
> 
> Chris's delegate actually implements NSOutlineViewDelegate, doesn't it?
> 
> Lee Ann's earlier suggestion of declaring TKOutlineViewDelegate to conform to 
> NSOutlineViewDelegate would prevent any remaining compiler complaints. Thus 
> the final declaration of the protocol would look like:
> 
>   @protocol TKOutlineViewDelegate 
> 
> Would that take care of your concern?


No. What Conrad is really suggesting is that there's a problem if the 
TKOutlineViewDelegate protocol has @required methods. For example, if you hand 
off the TKOutlineView to AppKit (for instance) and AppKit (for some reason) 
changes the delegate to some other NSOutlineViewDelegate-implementing object, 
it won't be implementing the required TKOutlineViewDelegate methods and will 
lead to an issue.

One answer to the problem is to never have required methods, but in practice I 
would think it's plenty safe to expect a delegate to not be swapped in the vast 
majority of cases. 


--
Seth Willits


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]

2013-03-26 Thread Quincey Morris
On Mar 26, 2013, at 13:11 , Conrad Shultz  wrote:

> If code expecting an NSOutlineView receives a TKOutlineView instance it may 
> break or behave unexpectedly since it may well try to set a delegate 
> conforming to  but you have made your class require a 
> delegate conforming to the more specific .

Chris's delegate actually implements NSOutlineViewDelegate, doesn't it?

Lee Ann's earlier suggestion of declaring TKOutlineViewDelegate to conform to 
NSOutlineViewDelegate would prevent any remaining compiler complaints. Thus the 
final declaration of the protocol would look like:

@protocol TKOutlineViewDelegate 

Would that take care of your concern?

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]

2013-03-26 Thread Chris Tracewell
On Mar 26, 2013, at 1:11 PM, Conrad Shultz  wrote:

> If code expecting an NSOutlineView receives a TKOutlineView instance it may 
> break or behave unexpectedly since it may well try to set a delegate 
> conforming to  but you have made your class require a 
> delegate conforming to the more specific .
> 
> This is why I earlier suggested declaring a -customDelegate (or whatever 
> you'd like to call it) property that would not interfere with the expected 
> delegate behavior.
> 
> -Conrad

I think I understand, but have I really required such code to conform to 
TKOutlineViewDelegate? Wouldn't it be in error to hand code expecting 
NSOutlineView a TKOutlineView? It seems that since TKOutlineView is a subclass 
of NSOutlineView that only code that handles TKOutlineView objects will gain 
these new delegate methods. In addition, those methods do check to make sure 
the delegate implements the method before I actually send to it. 

I'm obviously not fully clear on this topic so thanks in advance for any input.

CT
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]

2013-03-26 Thread Conrad Shultz

On Mar 26, 2013, at 12:58 PM, Chris Tracewell  wrote:

> On Mar 26, 2013, at 12:38 PM, Quincey Morris 
>  wrote:
> 
>> 2. Redeclare the "delegate" property:
>> 
>>  @interface TKOutlineView : NSOutlineView {}
>>  @property (nonatomic,readonly) id delegate;
>>  @end
>> 
>>  @implementation TKOutlineView
>>  @dynamic delegate;
>>  …
>>  [[self delegate]  outlineView:self enterKeyPressedForRow:[self 
>> selectedRow]];
>>  …
>> 
>> The @dynamic statement says "I don't have an implementation of the 
>> 'delegate' property in this class, but that's just fine because it's already 
>> implemented (in my superclass)".
> 
> Aha, success. Thank you - a very elegant and sensible solution.
> 
> CT


If code expecting an NSOutlineView receives a TKOutlineView instance it may 
break or behave unexpectedly since it may well try to set a delegate conforming 
to  but you have made your class require a delegate 
conforming to the more specific .

This is why I earlier suggested declaring a -customDelegate (or whatever you'd 
like to call it) property that would not interfere with the expected delegate 
behavior.

-Conrad
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]

2013-03-26 Thread Chris Tracewell
On Mar 26, 2013, at 12:38 PM, Quincey Morris 
 wrote:

> 2. Redeclare the "delegate" property:
> 
>   @interface TKOutlineView : NSOutlineView {}
>   @property (nonatomic,readonly) id delegate;
>   @end
> 
>   @implementation TKOutlineView
>   @dynamic delegate;
>   …
>   [[self delegate]  outlineView:self enterKeyPressedForRow:[self 
> selectedRow]];
>   …
> 
> The @dynamic statement says "I don't have an implementation of the 'delegate' 
> property in this class, but that's just fine because it's already implemented 
> (in my superclass)".

Aha, success. Thank you - a very elegant and sensible solution.

CT


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]

2013-03-26 Thread Chris Tracewell
On Mar 26, 2013, at 12:38 PM, Quincey Morris 
 wrote:

> 2. Redeclare the "delegate" property:
> 
>   @interface TKOutlineView : NSOutlineView {}
>   @property (nonatomic,readonly) id delegate;
>   @end
> 
>   @implementation TKOutlineView
>   @dynamic delegate;
>   …
>   [[self delegate]  outlineView:self enterKeyPressedForRow:[self 
> selectedRow]];
>   …
> 
> The @dynamic statement says "I don't have an implementation of the 'delegate' 
> property in this class, but that's just fine because it's already implemented 
> (in my superclass)".

Aha, success. Thank you - a very elegant and sensible solution.

CT


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]

2013-03-26 Thread Quincey Morris
On Mar 26, 2013, at 10:30 , Chris Tracewell  wrote:

> How can I get [self delegate] to recognize my custom methods?

There are two ways, one simpleminded, the other a bit sophisticated:

1. Use a local variable:

id delegate = [self delegate];
…
[delegate  outlineView:self enterKeyPressedForRow:[self selectedRow]];
…

You'd need to introduce the local variable into each scope where you send the 
delegate one of your custom messages.

2. Redeclare the "delegate" property:

@interface TKOutlineView : NSOutlineView {}
@property (nonatomic,readonly) id delegate;
@end

@implementation TKOutlineView
@dynamic delegate;
…
[[self delegate]  outlineView:self enterKeyPressedForRow:[self 
selectedRow]];
…

The @dynamic statement says "I don't have an implementation of the 'delegate' 
property in this class, but that's just fine because it's already implemented 
(in my superclass)".

Alternatively, you can avoid @dynamic by supplying an overriding implementation:

- (id) delegate {
return [super delegate];
}

but the @dynamic version is a bit more elegant.

With either alternative, this approach lets you avoid the clutter of the 
additional local variables.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]

2013-03-26 Thread Lee Ann Rucker

On Mar 26, 2013, at 10:30 AM, Chris Tracewell wrote:

> @protocol TKOutlineViewDelegate  //  because we need to 
> use "respondsToSelector"

Try 
@protocol TKOutlineViewDelegate 
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


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

2013-03-26 Thread Seth Willits
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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]

2013-03-26 Thread Chris Tracewell
On Mar 25, 2013, at 7:33 PM, Conrad Shultz  wrote:

> In the code you shared you had used "delegate" in one place and "[self 
> delegate]" in another; the second case is the correct one. If you replace all 
> naked uses of "delegate" with "[self delegate]" and you continue to get 
> compile-time errors please post your entire class or a reduced test case.

Okay, I am realizing that my original post was misleading about what the main 
issue is. The main point I should have made is that [self delegate] is not 
being allowed to handle my custom delegate methods. It is expecting a method 
from NSOutlineViewDelegate. The error I am getting is

No known instance method for selector 'outlineView:enterKetPressedForRow:'

Can I make a category on NSOutlineViewDelegate? Here's my original code with 
corrected [self delegate]


HEADER


#import 

@class TKOutlineView; // lets compiler know the @interface deleration is coming 
since we put @protocol before it

// delegate protocol
@protocol TKOutlineViewDelegate  //  because we need to use 
"respondsToSelector"
@optional
-(void)outlineView:(NSOutlineView *)theOutlineView 
enterKeyPressedForRow:(NSInteger)theRow;
-(void)outlineView:(NSOutlineView *)theOutlineView 
deleteKeyPressedForRow:(NSInteger)theRow;
@end

@interface TKOutlineView : NSOutlineView {}

@end



IMPLEMENTATION


#import "TKOutlineView.h"

@implementation TKOutlineView


-(void)keyDown:(NSEvent *)theEvent
{
if ([[theEvent characters] length] == 0)
{
// dead key
return;
}
else if ([[theEvent characters] length] == 1 && [[theEvent characters] 
characterAtIndex:0] == NSEnterCharacter && [[self delegate] 
respondsToSelector:@selector(outlineView: enterKeyPressedForRow:)]) 
{
[[self delegate]  outlineView:self enterKeyPressedForRow:[self 
selectedRow]];  /* ERROR  THIS LINE */
}
else if ([[theEvent characters] length] == 1 && [[theEvent characters] 
characterAtIndex:0] == NSDeleteCharacter && [[self delegate] 
respondsToSelector:@selector(outlineView: deleteKeyPressedForRow:)])
{
[[self delegate]  outlineView:self deleteKeyPressedForRow:[self 
selectedRow]];  /* ERROR  THIS LINE */
}
else 
{
[super keyDown:theEvent];
}
}

@end


How can I get [self delegate] to recognize my custom methods?

CT
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Master - Detail

2013-03-26 Thread Alex Kac
We've had a lot of success with this:
https://github.com/mattgemmell/MGSplitViewController

though we've customized the heck out of it too...

On Mar 26, 2013, at 10:34 AM, Fritz Anderson  wrote:

> On 25 Mar 2013, at 7:13 PM, koko  wrote:
> 
>> The Master - Detail template for an iOS app is a good place to start but, ….
>> 
>> … how does one size the split view to proportions dictated by the new app 
>> being developed?
> 
> You don't. UISplitViewController is hard-coded to make the master view 320 
> points wide. If you want something else, it's not that hard to code it 
> yourself.


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Master - Detail

2013-03-26 Thread Fritz Anderson
On 25 Mar 2013, at 7:13 PM, koko  wrote:

> The Master - Detail template for an iOS app is a good place to start but, ….
> 
> … how does one size the split view to proportions dictated by the new app 
> being developed?

You don't. UISplitViewController is hard-coded to make the master view 320 
points wide. If you want something else, it's not that hard to code it yourself.

— F

-- 
Fritz Anderson
Xcode 4 Unleashed: 4.5 supplement for free!
http://www.informit.com/store/xcode-4-unleashed-9780672333279


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

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

2013-03-26 Thread Diederik Meijer | Ten Horses
Dear list,

I need to create a grouped array from a simple array, it then populates a 
grouped UITableView. Below is my code, which works fine (the project uses ARC).

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.

  NSArray *resultArray = [[result objectForKey:@"hits"] objectForKey:@"hits"]; 
//1
  NSMutableSet *dates = [[NSMutableSet alloc] init]; //2
  for (NSDictionary *dict in resultArray) {
[dates addObject:[[dict objectForKey:@"_source"] 
objectForKey:@"datum_gepubliceerd_ymd"]]; //3
  }
  self.datesArray = [NSArray arrayWithArray:[[dates allObjects] 
sortedArrayUsingComparator:^(NSString* a, NSString* b) {
return [b compare:a options:NSNumericSearch]; //4
  }]];
  self.sortedJSONContent = [NSMutableArray array]; //5
  for (NSString *date in self.datesArray) {
NSMutableArray *sortedSection = [NSMutableArray array]; //6
for (NSDictionary *dict in resultArray) {
if ([[[dict objectForKey:@"_source"] 
objectForKey:@"datum_gepubliceerd_ymd"] isEqual:date])
[sortedSection addObject:dict]; //6
}
[self.sortedJSONContent addObject:sortedSection]; //7
}


These are the steps the above code takes:

1. Create the resultArray from a JSON web service, this is an array of 
dictionaries (result is returned from a NSJSONSerialization action);
2. Create a mutableSet to hold the group titles (these will go in the 
tableView's sectionHeader views);
3. Iterate through the resultArray and add the relevant key's value to the 
mutableSet;
4. Create a datesArray from the set, ordered descending (creating an array of 
unique values);
5. Create a new sortedJSONContent array to hold the grouped array when done;
6. Loop through the dates array (main loop), and with each iteration create a 
sortedSection array loop through the resultArray ('nested loop') and add those 
items for which the date is equal to the main loop's date (I don't need to 
worry about empty arrays here, because they only get created when a date 
exists, which means a record exists and, therefore, that the array will have at 
least one item);
7. Add the sortedSection array to the new sortedJSONContent array.

The output is an array of arrays that fits nicely into the grouped UITableView.

PS I know this is done much more easily using CoreData, I actually have another 
app that does just that. But in this case, all data is pulled in from the 
network at all times and there is no persistent storage on the device. For this 
reason, implementing CoreData feels like overkill to me.

Thanks!

Diederik
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com