« Q&A: March 17, 2001

Posted by Andy Monitzer on March 17, 2001 [Feedback (0) & TrackBack (0)]

// Question One

I'm writing a prettyprinter, and I want to have a method like:

    - (id)print:(NSString *)format, ...
In the implementation, I want to pass the format string and the args to stringWithFormat:, but I can't figure out the right syntax. K&R mentions some macros that can be used to step through the list of args, but I don't see how they apply here.


- (void)writeFormat:(NSString *)aFormat, ...;
     va_list argList;
     NSString *formattedString;

     va_start(argList, aFormat);
     formattedString = [[NSString alloc] initWithFormat:aFormat arguments:argList];
     [self writeString:formattedString];
     [formattedString release];

answer by: Ken Case

// Question Two

When you create a constant string using the @"" syntax, does it get added to the autorelease pool? For example if I did this:

NSString * foo = @"foo";
I would later do this:
[foo release];
But if I was passing it into a message like:
[textbox setStringValue:@"foo"];
I don't get a chance.

Should I do this:

[textbox setStringValue:[@"foo" autorelease]];


No. Since you didn't call init nor new, you must not release it (if you don't want to crash).
(Note: this is a general rule for all objects)

answer by: Andreas Monitzer

@"" should be neither released nor autoreleased. These are static strings that should not be freed.

answer by: Chris Kane

// Question Three

I want to write a Frond End to the UNIX-Application HXD, but I can't find out what class I have to use to let hxd and my Cocoa App communicate... I better like to use Java than Obj-C, is this possible?

Take a look at the NSTask/NSPipe class in Objective-C. Could be available via the Java bridge, otherwise you can use java.lang.Runtime.exec() and Process.

answer by: Helge Hess

// Question Four

Is there an easy way to convert a CFDictionary into a NSDictionary? What about other storage objects like this? How to go from CF to NS to OS??


All you need to do with most base classes is just cast.

If you have a CFStringRef foo, that is the same as NSString *foo.

CFStringRef == NSString *;
CFDictionaryRef == NSDictionary *

answer by: Eric Peyton

// Question Five

I catch a timestamp value in a NSEvent. I would like to convert this timestamp into double.


NSTimeInterval TimeStampToTimeInterval(UInt64 aTime) {
	UInt64 realTimeStamp;
	Nanoseconds nanoTimeStamp;
	AbsoluteTime absTimeStamp;
	NSTimeInterval timeInterval;	
	return timeInterval;

answer by: André Lipinski

// Question Six

I need to implement saving, opening, reading preferences for my Cocoa/ObjC app... I was wondering the best way to implement this?


Use NSUserDefaults, that way you use the OS X Defaults database.

answer by: Logan Hall

Post a comment