GUI-less Cocoa

iconara

Registered
I want to commit a heresy. I want to create a GUI-less Cocoa application.

I have heard rumors about what one must do to make this work. CocoaDevCentral tells me that I can't really make use of Foundation, but must rely on CoreFoundation, which seems not as sexy. I have however gotten the code below (here written from memory, so it may not be accurate) to compile and run...

Another rumor that I overheard was that one has to make a autorelease pool for autoreleasing to work, I have done this below, and I think it works, but not how or why.

Suggestions anyone? It's cool making commandline apps with OO-methods...

Code:
int main( ... ) {
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

   NSString *msg = [NSString stringWithString:@"running GUI-less"];

   printf( [msg cString] );

   [pool release];
}
 
Hi iconara,

I'm far from an expert in Cocoa (actually quite green) but might be able to clear up some of those rumors.
Using the Foundation Framework compared to the Application Kit Framework isn't as sexy but seems to have a rich feature set. Looking through the class list, you should be able to make something interesting to satisfy yourself.
Autorelease pools are setup by NSApplication when creating an app with the Application Framework. I couldn't find any info as to how and why.

Here are 2 altered examples I have from "Learning Cocoa" and Hillegass's "Cocoa Prog. for Mac OS X". They primarily make use of the collection classes. Hope they help to add clarity.

========
#import Foundation/Foundation.h

int main (int argc, const char * argv[])

{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *myArray;
NSString *myString;
NSMutableDictionary *myDict;
NSString *myotherString;
NSMutableArray *mykeyArray;



NSLog(@"Hello, World: This is a Foundation Tool.\n");

myArray = [[NSMutableArray alloc] init];

[myArray addObject: @"Here's a Freeckin String!"];

[myArray addObject: @"Yeah, and another?"];

[myArray addObject: @"Slap you in the face!"];

[myArray insertObject: @"Yo momma!!!" atIndex: 0];

[myArray exchangeObjectAtIndex: 0 withObjectAtIndex: 1];

NSLog (@"Array description: %@ items.\n", myArray);


myString = [myArray objectAtIndex: 2];

NSLog (@"The String description: %@ items.\n", myString);


myDict = [[NSMutableDictionary alloc] init];

[myDict setObject:mad:"stringOne" forKey:mad:"keyOne"];

[myDict setObject:mad:"stringTwo" forKey:mad:"keyTwo"];

[myDict setObject:mad:"stringThree" forKey:mad:"keyThree"];

[myDict setObject:mad:"stringFour" forKey:mad:"keyFour"];

NSLog (@"Dict description: %@ items.\n", myDict);

myotherString = [myDict objectForKey:mad:"keyOne"];

NSLog (@"String-Dict description: %@ items.\n", myotherString);


mykeyArray = [myDict allKeys];

NSLog (@"dictionary keys: %@ items.\n", mykeyArray);



[myArray release];
[myString release];
[myDict release];
[myotherString release];
[mykeyArray release];
[pool release];
return 0;
}

========
Aaron Hillegass's->
========

#import Foundation/Foundation.h

int main (int argc, const char * argv[])
{
NSMutableArray *array;
int i;
NSNumber *number;

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

array = [[NSMutableArray alloc] init];
for (i = 0; i < 11; i++) {
number = [[NSNumber alloc] initWithInt:(i*2)];
[array addObject:number];
[number release];
}

NSLog(@"array = %@", array);
[array release];

[pool release];
return 0;
}

========

The includes are missing the > characters. To lazy to turn off vB coding.
 
If you want to use event loops and such, which might come in handy if you have a lot of I/O, you'll want an NSRunLoop in there. You'll need to "prime the pump" so-to-speak so the run loop doesn't exit right away.

So your main(...) might look like this:
Code:
NSAutoreleasePool *pool;
    
// Thread startup maintenance
pool = [[NSAutoreleasePool alloc] init];
    
// Prime the run loop
[[NSRunLoop currentRunLoop] 
    addTimer:[NSTimer scheduledTimerWithTimeInterval:0 
        target:self 
        selector:@selector(runPrimaryTask:) 
        userInfo:nil 
        repeats:NO] 
    forMode:NSDefaultRunLoopMode];

// Start the run loop
[[NSRunLoop currentRunLoop] run];
    
// Clean up thread maintenance
[pool release];
[NSThread exit];

Just a thought.

I know there's that talk about using the core foundation. I don't know anything about that. Maybe you have to use CF instead of NS. I've just never tried it to be sure.

-Rob
 
Back
Top