Skip navigation.
 
mlWhat are the hazards of doing the following...
FROM : Jerry LeVan
DATE : Sat Nov 09 21:34:40 2002

Greetings,

I am scrambling to pick up Cocoa :)

Some of my methods are getting a bit long, I was wanting to see if I could
write some helper functions/procedures (not methods). I have not been able
to find any documentation in my books about intertwingling standard C code
with objective-c  ( although objective-c is supposed to be an extension of
C).

Here is my experiment: I took a small demo Cocoa app's implementation file
#import Controller.m
@implementation
:
<definitions of methods>
:
@end

And modified it to look like :
#import Controller.m
@implementation
:
void doSomething(NSString*);

-(void)awakeFromNib
{
doSomething(@"/usr/bin/cal");
}

:
<definitions of methods>
void doSomething(NSString* aString) {
  FILE *f;
  char mybuff[200];
    f = popen([aString cString], "r");
  if(f==NULL){
    NSLog(@"popen failed");
    exit(1);
  }
 
  while(fgets(mybuff,199,f) ) {
  NSLog([NSString stringWithCString:mybuff]);
  // printf(mybuff);
  }
  pclose(f);
}

@end

It worked properly, ie the calendar printed in the log window. It also
worked if I moved the subroutine to below the @end. (It also demos quick and
dirty way to grab the output of a unix command without using NSPipe.)

The rascal also works if the declaration of doSomething is placed into the
header file and removed from the implementation file.

I was a little surprised to see that stdio.h is evidently included by the
Cocoa headers.

1) Are there any general caveats about mixing C and Obj-C?
2) Is there a standard "place" for putting standard C definitions and code?

Thanks,

--Jerry
_______________________________________________
cocoa-dev mailing list | <email_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

Related mailsAuthorDate
mlWhat are the hazards of doing the following... Jerry LeVan Nov 9, 21:34
mlRe: What are the hazards of doing the following... Ondra Cada Nov 10, 20:01