Daily Snippet

Garbage Collection is deprecated. ARC it is now and so we have to update/change our Mac projects. Sometimes this is very easy and, of course, often it is harder. And sometimes it is even impossible.

And sometimes there is a need to provide code for both. This can be done with:

#if !__has_feature(objc_arc)
// retain, release etc. here
#endif

What Michael learned today

Never ever check for URLForUbiquityContainerIdentifier in a validateUserInterfaceItem call.

If Siri still is beta, than iCloud is alpha. That crap cost me two days until I realized that all me performance issues came from that. And do yourself a favor. Put those checks in a background thread. Especially at startup…

PLWeakCompatibility

Compatibility stubs to enable use of weak references with ARC on older OSes.

CocoaBox submitted for review

I’ve just submitted my latest App for review. CocoaBox is a sophisticated little tool for all iOS and OS X developers. Directly create new snippets with a hotkey out of and directly paste them into Xcode by keyboard with CocoaBox. Or attach unlimited files and tag and search for them.

And of course CocoaBox is iCloud enabled – means that you will have all your snippets and files in sync on all your Macs.

Currently two Apps of mine are stuck in review hell and I guess it has something to do with the iCloud support – but I don’t know what it is exactly and as always Apple is not able to communicate. So it may also take a while for CocoaBox to be approved :–(

-fno-objc-arc

As recently discovered – the iCloud and the sandboxing do not work without enabling ARC; or at least not when GC is enabled. Of course this is sometimes only a little bit of work, in most cases a lot of work and sometimes simply impossible for some resources or frameworks yet.

But there is a solution. One can simply disable ARC for those files:

Add compiler flag -fno-objc-arc in Targets > Build Phases > Compile Sources.

MOOMaskedIconView

Daily Snippet

Concurrency. And concurrency. And once again: concurrency. The holy grail to writing good Apps. And the hardest part. At least to debug.

The simplest way to performe a task not on the main thread is:

[self performSelectorInBackground:@selector(myConcurrentAction) withObject:nil];

- (void) myConcurrentAction {
NSAutoreleasePool *pool = [NSAutoreleasePool new];

// do whatever needs to be done
// but only all things NOT GUI
// ensure that nothing else tries to access used objects meanwhile

[pool drain];
}

Daily Snippet

I needed a way to terminate an application from within another without using Apple Script or old Carbon stuff. I finally found this nifty solution:

if ([[NSWorkspace sharedWorkspace] respondsToSelector:@selector(runningApplications)]) {
for (NSRunningApplication *app in [[NSWorkspace sharedWorkspace] runningApplications])
if ([@"Suggestions" isEqualToString:[app localizedName]])
[app terminate];
}

Daily Snippet

NSValueTransfomers are a very handy solution to store a lots of information in a simple int for example. Combined with bindings one can display text in OS X label colors for example. Something I needed today:

@implementation MGIntToColorTransformer

+ (Class)transformedValueClass {

return [NSColor class];
}

+ (BOOL)allowsReverseTransformation {

return NO;
}

- (id)transformedValue:(id)value {

NSColor *color = nil;

switch ([value intValue]) {
case 0:
color = [NSColor colorWithCalibratedRed:0.298 green:0.298 blue:0.298 alpha:1.000]; // [NSColor blackColor] || [NSColor clearColor]
break;
case 1:
color = [NSColor colorWithCalibratedRed:0.924 green:0.372 blue:0.337 alpha:1.000]; // red
break;
case 2:
color = [NSColor colorWithCalibratedRed:0.907 green:0.628 blue:0.255 alpha:1.000]; // orange
break;
case 3:
color = [NSColor colorWithCalibratedRed:0.845 green:0.771 blue:0.254 alpha:1.000]; // yellow
break;
case 4:
color = [NSColor colorWithCalibratedRed:0.642 green:0.766 blue:0.254 alpha:1.000]; // green
break;
case 5:
color = [NSColor colorWithCalibratedRed:0.335 green:0.599 blue:0.937 alpha:1.000]; // blue
break;
case 6:
color = [NSColor colorWithCalibratedRed:0.712 green:0.522 blue:0.796 alpha:1.000]; // lavender
break;
case 7:
color = [NSColor colorWithCalibratedRed:0.624 green:0.624 blue:0.624 alpha:1.000]; // gray
break;
default:
color = [NSColor colorWithCalibratedRed:0.298 green:0.298 blue:0.298 alpha:1.000];
break;
}

return color;
}

@end