« Q&A: February 28, 2001

Posted by Andy Monitzer on February 27, 2001 [Feedback (0) & TrackBack (0)]

// Question One

Is there any API in Cocoa like OpenTransport?

Answer

You're looking for a networking API, I presume? Well, there are plenty of choices.

The low-level API is BSD sockets - you'll find info about this in the manpages or on the 'net, since it's relevant to all BSDs.

There's NSSocketPort in Foundation, which is a fairly simple wrapper on BSD sockets. There's OmniNetworking, which you'll find at http://www.omnigroup.com/community/developer/, which is a great API for writing full-featured network apps like web browsers, news readers, etc. There are also a couple of other third-party networking frameworks (such as EDInternet).

If you're asking for something "like OpenTransport" because you're used to writing Carbon/Classic programs that use OpenTransport, you'll find that it exists on OS X too... and if you really want to use it from a Cocoa app, you might be able to.

answer by: Rick Roe


// Question Two

Currently I use [Date setStringValue:[NSCalendarDate date]]; (where Date is the instance variable of a text field i have) which returns 2001-02-24 etc etc, in my text field. This is more than I want to be displayed. How can I get it to return: "02/24/2001"?

Answer

myString = [myDate descriptionWithCalendarFormat:[NSUserDefaults
     standardUserDefaults stringForKey:NSShortDateFormatString]];

answer by: Greg Titus


// Question Three

What is the right Cocoa method to read keyboard? I am interested in reading especially Shift-Key, to do something like shift select graphic objects (something like Illustrator).

Answer

Override the selection method of that particular object (in most cases this will be -mouseDown:) and use something like

if ([theEvent modifierFlags] & NSAlphaShiftKeyMask)
	// handle shift select here

For more information see the documentation on NSEvent.

answer by: Marcel Bresink


// Question Four

I'm using an NSTableView to display some number results. The data shows up fine in the table view, but when I click in the table view or click in the NSScrollView to scroll the table, I get a crash (signal 10 (SIGBUS)).

Answer

Make sure that you are retaining the NSMutableArray that is your row datasource. It sounds like the array is valid the first time the table is refreshed (since you probably just set the array before you called reloadData?), but then is released and crashes at the next cell redraw.

answer by: Brian Hill


[Question Five];

I checked the document of NSString, but I didn't find any way to convert an int to an NSString.

Answers

int a;
return [NSString stringWithFormat:@"%d",a];

"%d" is the format representation for an integer. See some printf-documentation for more information.

answer by: Andreas Monitzer

You could also try

int anInt;
return [[NSValue valueWithInt:anInt] stringValue];
which might be cheaper than evaluating a format string.

answer by: John C. Randolph


Comments
Post a comment