Code for Terminal in Obj-C?

Sage
Posts: 1,199
Joined: 2004.10
Post: #16
ObjC without Cocoa ( or CoreFoundation ) is like Java without java.lang.* -- you can do it, but it isn't pretty.

You can do a nice ObjC commandline tool with just CoreFoundation.framework, but that depends on what you call "Cocoa". If Cocoa means NS* objects/functions then Foundation is still Cocoa.

You might as well ask if you can write a command-line C app without libc ...
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #17
TomorrowPlusX Wrote:ObjC without Cocoa ( or CoreFoundation ) is like Java without java.lang.* -- you can do it, but it isn't pretty.

You can do a nice ObjC commandline tool with just CoreFoundation.framework, but that depends on what you call "Cocoa". If Cocoa means NS* objects/functions then Foundation is still Cocoa.

You might as well ask if you can write a command-line C app without libc ...

CoreFoundation != Foundation

Have a look at Cocoa.h:

Code:
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <CoreData/CoreData.h>

Foundation is the framework which defines NSObject. If you want to write software which will compile on systems other than Mac OS X (Linux, for example), there are basically 2 alternatives. The first is to use GNUstep - an open-source implementation of NeXT's OpenStep specification.

If you don't feel like using NSObject as your base class, then you'll want to go with the second option: the Object class which is distributed with gcc. To use it, #import <objc/Object.h>.

Wikipedia has some nice examples of using the Object class:
http://en.wikipedia.org/wiki/Objective-C

To compile .m files with gcc, do the following:

gcc -lobjc *.m -o whateverYouWantToCallYourExecutable

edit: here's a nice example of using Object.h:
http://www.objc.info/samplecode/sample1/
Quote this message in a reply
Member
Posts: 153
Joined: 2004.12
Post: #18
Does NSObject have anything that the "Object" class does not?

There was a long silence...
'I claim them all,' said the Savage at last.
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #19
hangt5 Wrote:Does NSObject have anything that the "Object" class does not?

Object is now quite similar to NSObject. It supports +alloc and -init, just like NSObject (it used to use +new). Both fulfil the same purpose, but they do have their differences. Have a look at the header files for each if you're interested:

/usr/include/objc/Object.h
/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h

If you want to use things like NSString, NSDictionary, or NSArray, you need to go with NSObject. If you want to start from scratch and/or want to support platforms other than Mac OS X (like Linux, Solaris, or HP-UX), go with Object.

Edit: I just realized something fundamental to NSObject has which Object does not have: -retain and -release methods. I guess you'd have to subclass Object to get the retain count functionality.
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #20
Here's a subclass of Object I just wrote which implements the retain/release memory management you get with NSObject:

AGObject.h:
Code:
#import <objc/Object.h>

@interface AGObject : Object {
  unsigned long retainCount;
}
- (unsigned long)retainCount;
- (AGObject *)retain;
- (void)release;
- (void)dealloc;
@end

AGObject.m:
Code:
#import "AGObject.h"


@implementation AGObject

- (id)init {
  self = [super init];
  if (self != nil) {
    retainCount = 1;
  }
  return self;
}


- (unsigned long)retainCount {
  return retainCount;
}


- (AGObject *)retain {
  retainCount++;
  return self;
}


- (void)release {
  if (retainCount == 1) {
    [self dealloc];
  } else {
    retainCount--;
  }
}


- (void)dealloc {
  [self free];
}

@end
Quote this message in a reply
Post Reply