« Q&A: July 17, 2002

Posted by Jeff Hunter on July 17, 2002 [Feedback (1) & TrackBack (0)]

// Question One

I am creating a C-style array and would like to initialize all the elements of the array to 0. Right now I'm using a for loop, but is there a better way to do this?

Answer

You can quickly initialize a C-style array to zero in a couple of ways. For example, suppose you have an array of intergers named "array". When declaring this array you could write int array[100] = { 0 };. This takes advantage of the fact that when you initialize an array using a comma seperated list, any uninitialized elements are set to 0. Another possiblity is to call memset(array, 0, sizeof(array));. Read the memset man page to learn about this function.

answer by: Eric Albert on studentdev mailing list.

 

// Question Two

I have been trying to use the distributed notification center to pass information between a client and server application. The chatter between them is simple event based so I opted for this over a Distributed Objects style of IPC. Everything works just fine, for a while, after the chatter gets to a certain level, maybe >10-15 messages per second, the notifications just seem to disappear, completely. Once one is dropped, no more seem to pass through. This leads me to the assumption that somewhere a queue is getting filled up, freezes, and doesn't know what to do.

Answer

In 10.0 and 10.1, if the notification server gets an error, any error, in trying to send a notification to a client, including port queue full, the server purges all knowledge of that client. This is arguably a bug, but it does manage to keep people from using distributed notification as a high-volume communication channel, which it is not supposed to be (it can impact the system performance quite a bit). In the next OS release, the situation has been improved somewhat I think; I believe it will wait longer for space to become available in the queue, and I think the client-purge was eliminated, but I think the server might still decide to toss some notifications on the floor in high-volume situations. As an example, we can't have the server potentially using an open-ended amount of memory to keep notifications queued for clients which may have permanently gone out to lunch.

answer by: Chris Kane on cocoa-dev mailing list.

 

// Question Three

What is the glyph name for '#' (shift-3 on American keyboards)? It's not pound, hash, or even octothorpe.

Answer

It's [font glyphWithName:@"numbersign"].

answer by: Andre Lipinski on cocoa-dev mailing list.

 

// Question Four

I've got a Cocoa app, but no source code for it. Are there any tools that can examine the binary and try to extract class and other information from it?

Answer

Yes. First take a look at otool, which is included with the Dev Tools. otool can show you which frameworks a binary was linked against, among other things. Type "man otool" in a terminal window for detailed information on this tool. Secondly, I highly recommend you download class-dump. As its readme says, "class-dump is a utility for examining the Objective-C segment of Mach-O files. It generates the @interface and @protocol declarations for classes, categories and protocols." You can download class-dump here.

answer by: Piers on cocoa-dev mailing list.

 

// Question Five

I'm trying to "package" my app for deployment and want to use a custom background picture for the window that appears when the user double-clicks the volume that appears after being mounted from a .dmg file. The picture (a jpeg) appears in my disk image (having been set in the Show View Options window from the Finder; selecting the Picture radio button in the Background section. However, after ejecting the image and sending it to another computer, the picture is gone. I assume that the picture is stored somewhere on my system and not with the folder or volume. How can I make the picture stick with the volume?

Answer

Move the file onto the diskimage, and select that copy as your background image in Finder. Now, fire up the terminal and type: /Developer/Tools/SetFile -a V /Volumes/YourDmg/YourImage.jpg Eject the image and reinsert it and make sure that the image is there and that the image file is invisible.

answer by: David Remahl on cocoa-dev mailing list.

 

// Question Six

I have a MySQL database that I would like to interact with in my Cocoa application. I know there are C APIs for MySQL, but Objective-C is so cool that I'd rather use it. Are there any ways to access a MySQL database through an Objective-C API?

Answer

Serge Cohen and Bertrand Mansion are developing an Objective-C wrapper for the MySQL C API. You can download the latest version from the project's SourceForge page.

answer by: Stefan Keller on cocoa-dev mailing list.

 

// Question Seven

I've just bumped into the problem that -stringByResolvingSymlinksInPath doesn't resolve alias files, and that such alias files then say they are files even if they point to directories. Apparently I need to resolve such aliases by hand.

Answer

bDistributed has released BDAlias, which is a Cocoa class for dealing with aliases. You can grab BDAlias here.

answer by: Finlay Dobbie on cocoa-dev mailing list.

 

// Question Eight

Is there any way that I can create a menu extra similar to the AirPort, battery, and other menu extras that Apple includes with Mac OS X?

Answer

For future compatibility sake, you should consider using NSStatusBar and NSStatusItem instead of MenuExtras. You get just about all of the same behavior, but it requires an application to be running. There are some magic Info.plist keys to make the application not show up in the Dock or menu bar. You just use the Cocoa application template, add a controller, and in its awakeFromNib: method, you can create a new NSStatusItem in the [NSStatusBar systemStatusBar]. From there, you can set the menu, image, actions, etc. of the item.

answer by: Dan Waylonis on cocoa-dev mailing list.

 

// Question Nine

I'm writing some code which deals heavily with the web. I need to set HTTP fields such as USER-AGENT. Is there anyway to do this using NSURL or are things I need simply to specialized for them to be a part of NSURL?

Answer

NSURL is too minimalist for most applications. I suggest trying CURLHandle, which is a wrapper around CURL. It should be able to do most of the things you want. Download CURLHandle here.

answer by: Dan Wood on cocoa-dev mailing list.

 

// Question Ten

I noticed that most of the questions and answers on this page are taken from the cocoa-dev mailing list. I realize that cocoa-dev is a great source of information on Cocoa, but I don't have time to read cocoa-dev every day. When I'm looking for help with a specific problem are there any searchable archives of cocoa-dev?

Answer

I'm glad you asked. Please, please, please check out cocoa.mamasam.com for a searchable archive of cocoa-dev posts. This a a great resource for Cocoa programmers and will probably obsolete this series of Q&As. Go check it out now!


Comments

I have some code that would always work just fine before I upgraded to Xcode. It now says I have a parse error before the "*"

NSNumberFormatter *answerFormatter2 =
[[[NSNumberFormatter alloc] init] autorelease];
[answerFormatter2 setFormat:@"####.000;0.000;(#.##0.000)"];
[[Base cell] setFormatter:answerFormatter2];

How can I make this work.

Posted by: Matt on November 15, 2003 01:32 PM
Post a comment