How to Handle Array of Dictionaries when Archiving/Unarchiving?

2016-06-23 Thread Dave
Hi,

I have an Object that I Archive and Unarchive, e.g it conforms to the NSCopying 
and NSCoding Protocols. The Object in question contains NSArray and 
NSDictionary Object and I handle them like this:

initWithCoder:

myDictionary = [theCoder decodeObjectForKey:@"pLookUpDictionary "];
self.pLookUpDictionary = [[NSMutableDictionary alloc] 
initWithDictionary:myDictionary copyItems:NO];


encodeWithCoder:

[theCoder encodeBool: self.pLookUpDictionary forKey:@"pLookUpDictionary”];


copyWithZone:

myDictionaryCopy = [self.pLookUpDictionary mutableCopy];
myObjectCopy.pLookUpDictionary = myDictionaryCopy;


I now need to add another archivable property as so:

@property (nonatomic,strong)
NSMutableDictionary*>*
pLookUpDictionaryArray;

How do I handle this in the above three methods? Specially do I need to walk 
the Array and do a mutableCopy on each of the Dictionaries in the Array?

All the Best
Dave


___

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: How to Handle Array of Dictionaries when Archiving/Unarchiving?

2016-06-23 Thread Alastair Houghton
On 23 Jun 2016, at 12:21, Dave  wrote:
> 
> I have an Object that I Archive and Unarchive, e.g it conforms to the 
> NSCopying and NSCoding Protocols. The Object in question contains NSArray and 
> NSDictionary Object and I handle them like this:
> 
> initWithCoder:
> 
> myDictionary = [theCoder decodeObjectForKey:@"pLookUpDictionary "];
> self.pLookUpDictionary = [[NSMutableDictionary alloc] 
> initWithDictionary:myDictionary copyItems:NO];

You don’t need to do that.  If you encode a mutable dictionary, you’ll get a 
mutable dictionary back.  The only time that doesn’t happen is with plists or 
NSUserDefaults; with NS[Keyed]Archiver, you get what you gave it.

> I now need to add another archivable property as so:
> 
> @property (nonatomic,strong)  
> NSMutableDictionary*>*
> pLookUpDictionaryArray;
> 
> How do I handle this in the above three methods? Specially do I need to walk 
> the Array and do a mutableCopy on each of the Dictionaries in the Array?

No.  As long as your LTWClassX supports archiving, you can just tell the 
NSCoder to encode pLookUpDictionaryArray and it will handle the object graph 
for you.  It will even cope if your properties happen to refer to the same 
object more than once; it ensures that each object is stored only once, and 
that the resulting object graph is as it was when you saved it.

Kind regards,

Alastair.

--
http://alastairs-place.net


___

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

NSArrayController - error inserting object at arranged object index N

2016-06-23 Thread Jonathan Mitchell
The following raises with NSInternalInconsistencyException when :

1. an NSArrayController filter is in place, 
2. controller.clearsFilterPredicateOnInsertion = NO
3. added content object is rejected by the filter.

Is this behaviour by design?

#import “AppDelegate.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

NSArrayController *controller = [[NSArrayController alloc] 
initWithContent:@[@"foo"].mutableCopy];
controller.filterPredicate = [NSPredicate predicateWithBlock:^BOOL(id  
_Nonnull evaluatedObject, NSDictionary * _Nullable bindings) {
return [evaluatedObject isKindOfClass:[NSString class]];
}];
controller.clearsFilterPredicateOnInsertion = NO;
[controller addObject:@"bar"];
[controller addObject:@1]; // raises
}
@end
___

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: NSArrayController - error inserting object at arranged object index N

2016-06-23 Thread Jonathan Mitchell

> On 23 Jun 2016, at 14:27, Jonathan Mitchell  wrote:
> 
> The following raises with NSInternalInconsistencyException when :
> 
> 1. an NSArrayController filter is in place, 
> 2. controller.clearsFilterPredicateOnInsertion = NO
> 3. added content object is rejected by the filter.
> 
> Is this behaviour by design?
> 

It would seem there is some intention here according to the header:

- (void)insertObject:(id)object atArrangedObjectIndex:(NSUInteger)index;// 
inserts into the content objects and the arranged objects (as specified by 
index in the arranged objects) - will raise an exception if the object does not 
match all filters currently applied

-addObject: obviously calls  - insertObject: atArrangedObjectIndex: (the 
exception stack trace agrees)

The logic of this is a bit puzzling though as setting 
controller.clearsFilterPredicateOnInsertion == NO seems to designed to fail.
What’s the point of a filter that raises when it filters?

It is possible to catch the exception raised from -addObject: and proceed.
In some cases this may be acceptable but the object that was to be added won’t 
appear in the NSArrayController content collection which may be an issue if the 
filterPredicate is subsequently changed.

My workaround was to set controller.clearsFilterPredicateOnInsertion == YES and 
reset the filterPredicate following -addObject:

It might be possible to subclass NSArrayController but it might turn into a bit 
of a rabbit hole.

> #import “AppDelegate.h"
> 
> @interface AppDelegate ()
> 
> @property (weak) IBOutlet NSWindow *window;
> @end
> 
> @implementation AppDelegate
> 
> - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
> 
>NSArrayController *controller = [[NSArrayController alloc] 
> initWithContent:@[@"foo"].mutableCopy];
>controller.filterPredicate = [NSPredicate predicateWithBlock:^BOOL(id  
> _Nonnull evaluatedObject, NSDictionary * _Nullable bindings) {
>return [evaluatedObject isKindOfClass:[NSString class]];
>}];
>controller.clearsFilterPredicateOnInsertion = NO;
>[controller addObject:@"bar"];
>[controller addObject:@1]; // raises
> }
> @end
> ___
> 
> 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/lists%40mugginsoft.com
> 
> This email sent to li...@mugginsoft.com


___

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: NSArrayController - error inserting object at arranged object index N

2016-06-23 Thread Quincey Morris
On Jun 23, 2016, at 08:32 , Jonathan Mitchell  wrote:
> 
> It would seem there is some intention here according to the header:
> 
> - (void)insertObject:(id)object atArrangedObjectIndex:(NSUInteger)index;
> // inserts into the content objects and the arranged objects (as specified by 
> index in the arranged objects) - will raise an exception if the object does 
> not match all filters currently applied
> 
> -addObject: obviously calls  - insertObject: atArrangedObjectIndex: (the 
> exception stack trace agrees)
> 
> The logic of this is a bit puzzling …

Why is this puzzling? Arranged objects are *necessarily* filtered objects, so a 
would-be-filtered-out new object has no meaningful arranged object index. You 
could perhaps argue that ‘addObject:’ shouldn’t use  
‘insertObject:atArrangedObjectIndex:’ for exactly this reason, but since the 
current behavior is documented in the header it seems that the API has already 
picked its functional horse to run.

> My workaround was to set controller.clearsFilterPredicateOnInsertion == YES 
> and reset the filterPredicate following -addObject:
> 
> It might be possible to subclass NSArrayController …

There’s no need to work around at all. The correct, straightforward solution is 
to update the content array (i.e. the unfiltered objects being managed via the 
array controller) directly in a KVO compliant manner, using 
‘mutableArrayValueForKey:’.

The way you’ve shown your setup code fragment suggests you’re thinking of the 
content array being “inside” the array controller. I would suggest this is the 
wrong conceptualization. It’s much better to think of it is an indexed 
collection property of your data model, with the array controller merely being 
a glue object between parts of the MVC design.

Editorializing a bit further, if you’ll forgive me …

Your problem is a good example of why IMO it’s poor practice to use array 
controllers in code, except where you’re absolutely forced to. NS…Controller 
works fine as a standard connector piece between code and IB-based objects, but 
when you try to use it as API within a code design, you quickly run into 
inscrutable or inflexible behavior. You’ll end up writing more code *around* 
the NS…Controller than it would take to do what it does directly.

___

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

unnamed function586 $$ AMDRadeon X4000GLDriver

2016-06-23 Thread Jonathan Mitchell
On a rare occasion I encounter the following abort while rendering an image in 
an NSImage category.
The method (shown below) gets called frequently to render resources as 
grayscale.
Looks like the driver has issues but beyond that I am stuck.
Any ideas?

Thread 1 (main-thread)
#0  0x7fff9972ef06 in __pthread_kill ()
#1  0x00010183642d in pthread_kill ()
#2  0x7fff8d3426e7 in abort ()
#3  0x7fff9b3ace5c in gpusGenerateCrashLog ()
#4  0x0001127259dc in 
___lldb_unnamed_function586$$AMDRadeonX4000GLDriver ()
#5  0x7fff9b3ae204 in gpusSubmitDataBuffers ()
#6  0x000112762813 in 
___lldb_unnamed_function1027$$AMDRadeonX4000GLDriver ()
#7  0x000112778d37 in 
___lldb_unnamed_function1175$$AMDRadeonX4000GLDriver ()
#8  0x7fff91a1f22f in glReadPixels_Exec ()
#9  0x7fff91f0ceb9 in CI::GLContext::readback_bitmap(CI::Bitmap*, 
CI::swizzle_info) ()
#10 0x7fff91dd43d9 in CI::image_get_cgimage(CI::Context*, CI::Image*, 
CGRect, CGColorSpace*, CI::PixelFormat) ()
#11 0x7fff91db606a in -[CIContext 
createCGImage:fromRect:format:colorSpace:] ()
#12 0x7fff91db476e in -[CIContext drawImage:inRect:fromRect:] ()
#13 0x7fff9ba09801 in -[CIImage(NSAppKitAdditions) 
drawInRect:fromRect:operation:fraction:] ()
#14 0x7fff9ba09914 in -[CIImage(NSAppKitAdditions) 
drawAtPoint:fromRect:operation:fraction:] ()
#15 0x000100028f47 in -[NSImage(Test) 
bp_grayscaleImageWithAlphaValue:saturationValue:brightnessValue:contrastValue:] 
at 

NSImage category:

- (NSImage *)bp_grayscaleImageWithAlphaValue:(CGFloat)alphaValue
  saturationValue:(CGFloat)saturationValue
  brightnessValue:(CGFloat)brightnessValue
contrastValue:(CGFloat)contrastValue
{
NSSize size = [self size];
NSRect bounds = { NSZeroPoint, size };
NSImage *tintedImage = [[NSImage alloc] initWithSize:size];

[tintedImage lockFocus];

CIImage *image = [CIImage imageWithData:[self TIFFRepresentation]];
   
CIFilter *colorFilter = [CIFilter filterWithName:@"CIColorControls"];
[colorFilter setDefaults];
[colorFilter setValue:image forKey:kCIInputImageKey];
[colorFilter setValue:[NSNumber numberWithFloat:saturationValue]  
forKey:kCIInputSaturationKey];
[colorFilter setValue:[NSNumber numberWithFloat:brightnessValue] 
forKey:kCIInputBrightnessKey];
[colorFilter setValue:[NSNumber numberWithFloat:contrastValue] 
forKey:kCIInputContrastKey];

CIImage *filterImage = [colorFilter valueForKey:kCIOutputImageKey];

[filterImage drawAtPoint:NSZeroPoint // << this is the call site that 
eventually aborts
fromRect:bounds
   operation:NSCompositeCopy
fraction:alphaValue]; 

[tintedImage unlockFocus];

return tintedImage;
}


___

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: NSArrayController - error inserting object at arranged object index N

2016-06-23 Thread Jonathan Mitchell

> On 23 Jun 2016, at 17:51, Quincey Morris 
>  wrote:
> 
> 
>> My workaround was to set controller.clearsFilterPredicateOnInsertion == YES 
>> and reset the filterPredicate following -addObject:
>> 
>> It might be possible to subclass NSArrayController …
> 
> There’s no need to work around at all. The correct, straightforward solution 
> is to update the content array (i.e. the unfiltered objects being managed via 
> the array controller) directly in a KVO compliant manner, using 
> ‘mutableArrayValueForKey:’.
> 

Do you mean something like this?

NSMutableArray *proxy = [NSArrayController mutableArrayValueForKey:@"content"];
[proxy addObject:object];
[proxy filterUsingPredicate:self.filterPredicate];

This does work but is very slow even with a small collection (the subclass 
returned is an instance of NSKeyValueSlowMutableArray).
Perhaps NSArrayController does not override all the required KVC  methods for 
the content key?

[self.submissionsArrayController addObject:object];
self.submissionsArrayController.filterPredicate = self.filterPredicate;

The above amounts to the same thing and seems much more performant.

I suppose I could make my NSViewController class KVC compliant for the content 
array but that seems overkill in this case.

Thanks

J


___

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: unnamed function586 $$ AMDRadeon X4000GLDriver

2016-06-23 Thread Ken Thomases
On Jun 23, 2016, at 2:56 PM, Jonathan Mitchell  wrote:
> 
> On a rare occasion I encounter the following abort while rendering an image 
> in an NSImage category.
> The method (shown below) gets called frequently to render resources as 
> grayscale.
> Looks like the driver has issues but beyond that I am stuck.
> Any ideas?
> 
> Thread 1 (main-thread)
> #00x7fff9972ef06 in __pthread_kill ()
> #10x00010183642d in pthread_kill ()
> #20x7fff8d3426e7 in abort ()
> #30x7fff9b3ace5c in gpusGenerateCrashLog ()

Are there any logs generated from the above call (other than your app's crash 
log)?  Browse around within Console.app's log list to see.

> #40x0001127259dc in 
> ___lldb_unnamed_function586$$AMDRadeonX4000GLDriver ()
> #50x7fff9b3ae204 in gpusSubmitDataBuffers ()
> #60x000112762813 in 
> ___lldb_unnamed_function1027$$AMDRadeonX4000GLDriver ()
> #70x000112778d37 in 
> ___lldb_unnamed_function1175$$AMDRadeonX4000GLDriver ()
> #80x7fff91a1f22f in glReadPixels_Exec ()
> #90x7fff91f0ceb9 in CI::GLContext::readback_bitmap(CI::Bitmap*, 
> CI::swizzle_info) ()
> #10   0x7fff91dd43d9 in CI::image_get_cgimage(CI::Context*, CI::Image*, 
> CGRect, CGColorSpace*, CI::PixelFormat) ()
> #11   0x7fff91db606a in -[CIContext 
> createCGImage:fromRect:format:colorSpace:] ()
> #12   0x7fff91db476e in -[CIContext drawImage:inRect:fromRect:] ()
> #13   0x7fff9ba09801 in -[CIImage(NSAppKitAdditions) 
> drawInRect:fromRect:operation:fraction:] ()
> #14   0x7fff9ba09914 in -[CIImage(NSAppKitAdditions) 
> drawAtPoint:fromRect:operation:fraction:] ()
> #15   0x000100028f47 in -[NSImage(Test) 
> bp_grayscaleImageWithAlphaValue:saturationValue:brightnessValue:contrastValue:]
>  at 
> 
> NSImage category:
> 
> - (NSImage *)bp_grayscaleImageWithAlphaValue:(CGFloat)alphaValue
>  saturationValue:(CGFloat)saturationValue
>  brightnessValue:(CGFloat)brightnessValue
>contrastValue:(CGFloat)contrastValue
> {
>NSSize size = [self size];
>NSRect bounds = { NSZeroPoint, size };
>NSImage *tintedImage = [[NSImage alloc] initWithSize:size];
> 
>[tintedImage lockFocus];
> 
>CIImage *image = [CIImage imageWithData:[self TIFFRepresentation]];
> 
>CIFilter *colorFilter = [CIFilter filterWithName:@"CIColorControls"];
>[colorFilter setDefaults];
>[colorFilter setValue:image forKey:kCIInputImageKey];
>[colorFilter setValue:[NSNumber numberWithFloat:saturationValue]  
> forKey:kCIInputSaturationKey];
>[colorFilter setValue:[NSNumber numberWithFloat:brightnessValue] 
> forKey:kCIInputBrightnessKey];
>[colorFilter setValue:[NSNumber numberWithFloat:contrastValue] 
> forKey:kCIInputContrastKey];

Any chance these values are out of valid range?

> 
>CIImage *filterImage = [colorFilter valueForKey:kCIOutputImageKey];
> 
>[filterImage drawAtPoint:NSZeroPoint // << this is the call site that 
> eventually aborts
>fromRect:bounds
>   operation:NSCompositeCopy
>fraction:alphaValue]; 

What does it mean to use a potentially non-1.0 fraction for a copy to an 
as-yet-uninitialized image?  No idea if that might contribute to the problem.  
Maybe clearing the image to some background color first would help.

Also, have you considered using -[CIContext createCGImage:fromRect:] to create 
the image instead of drawing into a focus-locked NSImage?  To get an NSImage 
from the CGImage, you can use [[NSImage alloc] initWithCGImage:… 
size:NSZeroSize].

Regards,
Ken


___

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: unnamed function586 $$ AMDRadeon X4000GLDriver

2016-06-23 Thread Jonathan Mitchell

> On 23 Jun 2016, at 21:39, Ken Thomases  wrote:
> 
> O
> 
> Are there any logs generated from the above call (other than your app's crash 
> log)?  Browse around within Console.app's log list to see.
Had a poke around but nothing significant that I can see.
> 
>>   CIFilter *colorFilter = [CIFilter filterWithName:@"CIColorControls"];
>>   [colorFilter setDefaults];
>>   [colorFilter setValue:image forKey:kCIInputImageKey];
>>   [colorFilter setValue:[NSNumber numberWithFloat:saturationValue]  
>> forKey:kCIInputSaturationKey];
>>   [colorFilter setValue:[NSNumber numberWithFloat:brightnessValue] 
>> forKey:kCIInputBrightnessKey];
>>   [colorFilter setValue:[NSNumber numberWithFloat:contrastValue] 
>> forKey:kCIInputContrastKey];
> 
> Any chance these values are out of valid range?
No. Everything looks sane. The range of arguments passed into the method is 
very small so there isn’t much variation.
> 
>> 
>>   CIImage *filterImage = [colorFilter valueForKey:kCIOutputImageKey];
>> 
>>   [filterImage drawAtPoint:NSZeroPoint // << this is the call site that 
>> eventually aborts
>>   fromRect:bounds
>>  operation:NSCompositeCopy
>>   fraction:alphaValue]; 
> 
> What does it mean to use a potentially non-1.0 fraction for a copy to an 
> as-yet-uninitialized image?  No idea if that might contribute to the problem. 
>  Maybe clearing the image to some background color first would help.
> 
> Also, have you considered using -[CIContext createCGImage:fromRect:] to 
> create the image instead of drawing into a focus-locked NSImage?  To get an 
> NSImage from the CGImage, you can use [[NSImage alloc] initWithCGImage:… 
> size:NSZeroSize].

Thanks for the suggestions I will give them a go.

J


___

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: NSArrayController - error inserting object at arranged object index N

2016-06-23 Thread Quincey Morris
On Jun 23, 2016, at 13:39 , Jonathan Mitchell  wrote:
> 
> Do you mean something like this?
> 
> NSMutableArray *proxy = [NSArrayController 
> mutableArrayValueForKey:@"content"];

That wasn’t what I had in mind. I meant that you would make the array a 
property of a real model object, and update *that* property, without referring 
to the array controller directly at all. If you do a KVO compliant update under 
those circumstances, the array controller will get a KVO notification and 
update its internal state automatically. You shouldn’t need to re-filter 
manually.

In theory, that allows the array controller to optimize what it does with the 
update. If you just add one object, it shouldn’t need to rescan everything in 
the array, or apply the filter predicate to anything except the object you 
added. (Caveat: it’s a black box, so we don’t know what it does, but we HOPE 
that it does this efficiently.)

___

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