• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
What are the hazards of doing the following...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

What are the hazards of doing the following...


  • Subject: What are the hazards of doing the following...
  • From: Jerry LeVan <email@hidden>
  • Date: Sat, 09 Nov 2002 15:34:40 -0500

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@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: What are the hazards of doing the following...
      • From: Ondra Cada <email@hidden>
  • Prev by Date: Re: Calling a unix command from Cocoa ?
  • Next by Date: Using a subclass in IB
  • Previous by thread: HELP : contex menu and mouseMoved events
  • Next by thread: Re: What are the hazards of doing the following...
  • Index(es):
    • Date
    • Thread