• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: This CFDictionaryRef casting works, but it's ugly (or, please help me clean up my code!)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: This CFDictionaryRef casting works, but it's ugly (or, please help me clean up my code!)
      • From: Chris Gregg <email@hidden>
    • Re: This CFDictionaryRef casting works, but it's ugly (or, please help me clean up my code!)
      • From: "Shawn Erickson" <email@hidden>
References: 
 >RE: How do I find out if a process is running if I have the process Name? (From: "Christopher Hickman" <email@hidden>)
 >This CFDictionaryRef casting works, but it's ugly (or, please help me clean up my code!) (From: Chris Gregg <email@hidden>)

  • Prev by Date: Re: Can I assume uninitialized variable's value to be nil or 0?
  • Next by Date: Re: This CFDictionaryRef casting works, but it's ugly (or, please help me clean up my code!)
  • Previous by thread: This CFDictionaryRef casting works, but it's ugly (or, please help me clean up my code!)
  • Next by thread: Re: This CFDictionaryRef casting works, but it's ugly (or, please help me clean up my code!)
  • Index(es):
    • Date
    • Thread