« Q&A: April 09, 2001

Posted by Andy Monitzer on April 09, 2001 [Feedback (0) & TrackBack (0)]

// Question One

What is the right way to determine if a given file/directory is a file package?


[[NSWorkspace sharedWorkspace] isFilePackageAtPath:fullPath];

answer by: Douglas Davidson

// Question Two

Does anyone happen to know, if it is possible to "dump" somehow or read somewhere the mapping the Finder maintains between, CFBundleOSTypes, CFBundleTypeExtensions and CFBundleTypeIconFile and the application(-path)?


You might look at using the functions in the LaunchServices framework, which is a subframework af the ApplicationServices framework. There is a function in there that can retrieve the application to use to open a given FSRef. I don't think there's anyway to get a complete dump of all the registered types, but you can test individual cases. The file where the binding are stored is the LSClaimedTypes file in ~/Library/Preferences, but heck if I know what format it's in.

answer by: Brian Webster

// Question Three

Is there a standard for command key bindings in OSX? It seems like every app has a different key-combo for preferences and the like? Is cmd-shift supposed to be for certain operations while cmd-opt for others? Has Apple written any specs on this? Are some key combinations reserved?


Most of this is covered in the Document


answer by: Vince DeMarco

// Question Four

What is the function that gives the time in in finer resolution than seconds? I want to check how long some code takes to execute.


NSDate *start;
NSTimeInterval elapsed; // (NSTimeInterval is defined as a double)

start = [NSDate date];
elapsed = -[start timeIntervalSinceNow];

answer by: Alex Karahalios

// Question Five

Is it possible to use pthreads based code inside a Cocoa app?


Traditional posix threads (pthreads) and NSThreads are somewhat interchangeable; there are some rules of operation, though, restrictions that you need to follow if you want things to work reliably.

First restriction, AppKit drawing. You need to use a special method for creating an AppKit-based thread in order to do display stuff. In NSApplication.h,

+ (void)detachDrawingThread:(SEL)selector toTarget:(id)target  withObject:(id)argument;

Now, there will be people who will tell you that this isn't necessary, that their method (x) worked without any problems. I can't claim to know the rules, or whether or not this is historical, but its existence signifies a likely difference from your average detachNewThreadSelector call.

Second restriction. In the case of more general threading, the first use of NSThread will notify your NSApplication that it needs to be in multithread mode; so if you want to use posix from within your app, create a dead NSThread that returns and closes the thread to get all of the flags set off for your app. Thread-safety setup is set up on first NSThread creation. Spin off a thread to do something trivial (or to do nothing at all. :) to do that setup. The common case is:

[NSThread detachNewThreadSelector:@selector(self) toTarget:anyObject withObject:nil];

Which, of course, will detach a new thread to any object (replace anyObject with any object) and with nothing.

answer found by: Gregory Block

Post a comment