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: Chris Gregg <email@hidden>
- Date: Thu, 18 May 2006 23:16:49 -0400
- Thread-topic: This CFDictionaryRef casting works, but it's ugly (or, please help me clean up my code!)
Thanks, Shawn! I took your suggestion and changed it to use the bundle
name, and it works great (and looks oh-so-much-better):
NSDictionary* appEntry;
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
NSArray *activeApps = [ws launchedApplications];
NSEnumerator *enumerator = [activeApps objectEnumerator];
BOOL iWebRunning = NO;
while(appEntry = [enumerator nextObject]) {
NSString* appBundle = [appEntry
objectForKey:@"NSApplicationBundleIdentifier"];
if ([@"com.apple.iWeb" isEqualToString:appBundle]) {
iWebRunning = YES;
break;
}
}
I'm just beginning to understand the toll-free bridging, so thanks for
clarifying it. Cheers!
-Chris Gregg
On 5/18/06 11:03 PM, "Shawn Erickson" <email@hidden> wrote:
> 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