NSMutableArray initWithCapacity nuances

Matt Gallagher has written a pretty informative article on Cocoa’s collection classes, along with a couple of benchmarks (with & without initWithCapacity:, as well as cross class comparisons) http://cocoawithlove.com/2008/08/nsarray-or-nsset-nsdictionary-or.html His test (source available) for an NSMutableArray of length 1,000,000 took 0.582256sec without capacity and just 0.572139sec with capacity. Test | Time [NSMutableArray array] | 0.582256 … Read more

Is there a “right” way to have NSTextFieldCell draw vertically centered text?

The other answers didn’t work for multiple lines. Therefore I initially continued using the undocumented cFlags.vCentered property, but that caused my app to be rejected from the app store. I ended up using a modified version of Matt Bell’s solution that works for multiple lines, word wrapping, and a truncated last line: -(void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView … Read more

How can I duplicate, or copy a Core Data Managed Object?

Here’s a class I created to perform a “deep copy” of managed objects: attributes and relationships. Note that this does not check against loops in the object graph. (Thanks Jaanus for the starting off point…) @interface ManagedObjectCloner : NSObject { } +(NSManagedObject *)clone:(NSManagedObject *)source inContext:(NSManagedObjectContext *)context; @end @implementation ManagedObjectCloner +(NSManagedObject *) clone:(NSManagedObject *)source inContext:(NSManagedObjectContext *)context{ … Read more

Deep copy of dictionaries gives Analyze error in Xcode 4.2

Presumably, it is because deepCopy does not begin with the prefix copy. So you may want to change to something like copyWithDeepCopiedValues (or something like that), and then see if the analyzer flags that. Update As Alexsander noted, you can use attributes to denote reference counting intent. This should (IMO) be the exception to the … Read more

Not being able to edit NSTextField on NSPopover even though Editable behavior is set

Not sure if you still need the answer, but there may be some others still looking. I found a solution on apple developer forums. Quoting the original author: The main problem is the way keyboard events works. Although the NSTextField (and all its superviews) receives keyboard events, it doesn’t make any action. That happens because … Read more

Observing an NSMutableArray for insertion/removal

But shouldn’t the synthesized accessors automatically return such a proxy object? No. What’s the proper way to work around this–should I write a custom accessor that just invokes [super mutableArrayValueForKey…]? No. Implement the array accessors. When you call these, KVO will post the appropriate notifications automatically. So all you have to do is: [myObject insertObject:newObject … Read more

How to save a NSImage as a new file

You could add a category to NSImage like this @interface NSImage(saveAsJpegWithName) – (void) saveAsJpegWithName:(NSString*) fileName; @end @implementation NSImage(saveAsJpegWithName) – (void) saveAsJpegWithName:(NSString*) fileName { // Cache the reduced image NSData *imageData = [self TIFFRepresentation]; NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData]; NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor]; imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps]; [imageData writeToFile:fileName atomically:NO]; } @end … Read more

How well is Objective-C++ supported?

Disclaimer: I don’t work or speak for Apple, so this is my opinion: I can’t speak for the major dev shops, but in my small group, we’ve used Objective-C++ both for integrating C++ libraries, and as you propose for writing backends in C++. As @alxp mentions, things like exception handling across the language boundary are … Read more