Re: This CFDictionaryRef casting works, but it's ugly (or, please help me clean up my code!)
Re: This CFDictionaryRef casting works, but it's ugly (or, please help me clean up my code!)
- Subject: Re: This CFDictionaryRef casting works, but it's ugly (or, please help me clean up my code!)
- From: "Shawn Erickson" <email@hidden>
- Date: Thu, 18 May 2006 20:03:07 -0700
On 5/18/06, Chris Gregg <email@hidden> wrote:
This is a very newbie question about casting and C mixing with Cocoa. The
following code works, with no errors or warnings, but I am very queasy about
all of the typecasting, and I would love some help standardizing it a bit.
I have tried working through the documentation, but I can't figure out what
to change...thanks!
// Determine if iWeb is currently running
BOOL iWebRunning = NO;
CFDictionaryRef appName;
NSArray *appArray; // the docs say this should be a CFDictionaryRef...
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
NSArray *activeApps = [ws launchedApplications];
NSEnumerator *enumerator = [activeApps objectEnumerator];
while (appArray = [enumerator nextObject]) {
appName = CFDictionaryGetValue ((CFDictionaryRef)appArray,
CFSTR("NSApplicationName"));
if ([(NSString *)appName isEqualTo:@"iWeb"]) iWebRunning = YES;
}
Why are you bringing CFDictionaryGetValue into this code in the first
place? I don't see documentation that makes CFDictionaryRef claim it
says NSDictionary in mine... anyway CFDictionaryRef is toll-free
bridged so it acts just like an NSDictionary in Cocoa no need to cast
it back to a CFDictionaryRef.
...
NSDictionary* appEntry;
...
while(appEntry = [enumerator nextObject]) {
NSString* appName = [appEntry objectForKey:@"NSApplicationName"];
if ([@"iWeb" isEqualToString:appName]) {
iWebRunning = YES;
break;
}
}
I also suggest that you use the bundle identifier for iWeb
("com.apple.iWeb") instead of the applications name since that is a
better unique key for an application on Mac OS X. You get the bundle
identifier using @"NSApplicationBundleIdentifier" as the key.
-Shawn
(note the above was written email from memory and not compiled)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden